问题描述
我必须将每个配置文件名称存储在一个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;
解决方法
目前,我还没有办法对此进行测试,但是基于您在图像中显示的输出,您似乎可以将所有输出,将其拆分为单独的行,将每行拆分为':'
字符,然后从该拆分中选择第二部分以获取名称。
但是首先,我认为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
(这是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);