首页 > 解决方案 > 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;

标签: c#wifinetshssid

解决方案


我目前没有办法对此进行测试,但根据您在图像中显示的输出,您似乎可以获取所有输出,将其拆分为单独的行,拆分':'字符上的每一行,然后然后从该拆分中选择第二部分以获取名称。

但首先,我认为 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);

推荐阅读