c# - C# 将每个 wlan 配置文件存储在 ObservableCollection 中
问题描述
我必须将每个配置文件名称存储在 Observable 集合中,但我不知道该怎么做,我做了这个项目的很大一部分,但它是如何访问每个我不知道的配置文件名称做。
我看到人们在使用 Substrings 和 IndexOf,我试过了,但问题是我要显示的配置文件名称不止一个,所以这不起作用。
我按照本教程进行操作:https ://www.youtube.com/watch?v=Yr3nfHiA8Kk但它显示了如何使用当前连接的 Wifi
InitializeComponent();
ObservableCollection<String> reseaux = new ObservableCollection<String>();
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "netsh.exe";
//p.StartInfo.Arguments = "wlan show interfaces";
p.StartInfo.Arguments = "wlan show profile";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
/*foreach (System.Diagnostics.Process profile in profile)
{
reseaux.Add(reseauName);
}*/
lesReseaux.ItemsSource = reseaux;
解决方案
我目前没有办法对此进行测试,但根据您在图像中显示的输出,您似乎可以获取所有输出,将其拆分为单独的行,拆分':'
字符上的每一行,然后然后从该拆分中选择第二部分以获取名称。
但首先,我认为 to 的论点show
是"profiles"
(复数),根据其中一条评论,您可能需要使用netsh.exe
. 因此该代码可能如下所示:
var startInfo = new ProcessStartInfo
{
FileName = Path.Combine(Environment.SystemDirectory, "netsh.exe"),
Arguments = "wlan show profiles",
UseShellExecute = false,
RedirectStandardOutput = true,
};
var p = Process.Start(startInfo);
p.WaitForExit();
之后,命令的输出将存储在p.StandardOutput
(即 a StreamReader
)中,我们可以使用以下命令将其全部作为字符串获取.ReadToEnd()
:
var output = p.StandardOutput
// Get all the output
.ReadToEnd()
// Split it into lines
.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
// Split each line on the ':' character
.Select(line => line.Split(new[] {':'}, StringSplitOptions.RemoveEmptyEntries))
// Get only lines that have something after the ':'
.Where(split => split.Length > 1)
// Select and trim the value after the ':'
.Select(split => split[1].Trim());
现在我们有了一个IEnumerable<string>
名字,我们可以用它来初始化我们的集合:
var reseaux = new ObservableCollection<string>(output);
推荐阅读
- reactjs - axios.create 时更改不记名令牌
- javascript - 如何获得两个日期之间的确切月份数?
- node.js - 处理基于角色的权限时在哪里存储角色?
- haskell - 如何调试被“杀死”的堆栈进程?
- c# - SqlException:对象名无效,对象在运行时创建
- android - YoutubeExtractor Android 依赖错误
- php - 包含来自 Head 标签的脚本和资源的 PHP 文件(PHP 包含问题)
- reactjs - 在 ReactJS 中对表格进行分页
- next.js - __dirname 未在 API 路由中定义
- html - 我无法为我的身体设置颜色背景