C# 如何从netsh的标准输出中提取特定字符串?

C# 如何从netsh的标准输出中提取特定字符串?,c#,regex,list,netsh,C#,Regex,List,Netsh,我正在使用netsh检查保存的无线配置文件及其加密状态。我可以像这样捕获netsh的输出: private void wifiButton_Click(object sender, EventArgs e) { Process cmd = new Process(); cmd.StartInfo.FileName = "netsh.exe"; System.Threading.Thread.Sleep(50); c

我正在使用
netsh
检查保存的无线配置文件及其加密状态。我可以像这样捕获
netsh
的输出:

   private void wifiButton_Click(object sender, EventArgs e)
    {


        Process cmd = new Process();
        cmd.StartInfo.FileName = "netsh.exe";
        System.Threading.Thread.Sleep(50);
        cmd.StartInfo.Arguments = "wlan show profiles";
        cmd.StartInfo.UseShellExecute = false;
        cmd.StartInfo.RedirectStandardOutput = true;
        cmd.StartInfo.RedirectStandardError = true;
        cmd.Start();
        //* Read the output (or the error)
        string output = cmd.StandardOutput.ReadToEnd();
        textBox3.Text = output;
        cmd.WaitForExit();          
    }
结果返回一个文本框,如下所示:

Profiles on interface Wi-Fi:

Group policy profiles (read only)
---------------------------------
<None>

User profiles
-------------
All User Profile     : GuestFSN
All User Profile     : CorporateWifi
All User Profile     : ATT3122
接口Wi-Fi上的配置文件: 组策略配置文件(只读) --------------------------------- 用户配置文件 ------------- 所有用户配置文件:GuestFSN 所有用户配置文件:CorporateWifi 所有用户配置文件:ATT3122 我想取出无线配置文件名称(GuestFSN、CorporateWifi、ATT3122等),并将它们放入列表中。我如何在C#中做到这一点?

您正在寻找的。Regex允许您定义要在较大字符串中查找的模式。这将允许您从标准输出返回的字符串中提取字符串列表(网络名称)

要匹配模式“所有用户配置文件[空白]:[名称]”,可以使用以下正则表达式模式:

所有用户配置文件[\s]+:(.*)

如果满足以下条件,则在此模式的较大字符串中找到匹配项:

  • 出现文本字符串“所有用户配置文件”
  • 后跟一个或多个空白字符-
    [\s]+
  • 后跟冒号和空格
  • 后跟长度不确定的字符串-
    (*)
    (但以换行符结尾)
您可以使用类似于的工具测试此正则表达式模式

下面是您的场景的代码示例:

var regex = new Regex(@"All User Profile[\s]+: (.*)");
var resultList = new List<string>();

foreach (Match match in regex.Matches(output))
{
    resultList.Add(match.Groups[1]);
}
var regex=new regex(@“所有用户配置文件[\s]+:(*);
var resultList=新列表();
foreach(regex.Matches中的匹配(输出))
{
结果列表.Add(match.Groups[1]);
}
使用
foreach
循环,以便我们可以处理
regex.Matches
中的一个或多个结果,并将它们全部添加到结果列表中

这里有一把小提琴来演示:

使用系统;
使用System.Collections.Generic;
使用System.Text.RegularExpressions;
公共课程
{
公共静态void Main()
{
//输出将由前面的代码设置
接口Wi-Fi上的var output=@“配置文件:
组策略配置文件(只读)
---------------------------------
用户配置文件
-------------
所有用户配置文件:GuestFSN
所有用户配置文件:CorporateWifi
所有用户配置文件:ATT3122”;
var regex=new regex(@“所有用户配置文件[\s]+:(.*));
var resultList=新列表();
foreach(regex.Matches中的匹配(输出))
{
结果列表.Add(match.Groups[1].ToString());
}
Console.WriteLine(string.Join(“,”,resultList));
}
}

那么您的问题是什么?类似于“C#string类是否具有
IndexOf
?”或“使用正则表达式查找字符串”…
C#
的限制-没有这样的事情。无论您通过命令行执行什么操作,都可能会调用Win32 API,您可以使用P/Invoke从C代码访问这些API。我不知道你所说的“C#的局限性”是什么意思。还有很多工作要做,将管理访问内置到应用程序中,然后运行本机Wifi API来获取无线数据,之后我仍然无法从我所做的研究中查询保存的无线配置文件数据。在这里,这似乎相当直截了当。我运行netsh,获取配置文件名,将名称隔离到一个列表中,然后BAM再次通过netsh逐个运行它们。@AlexeiLevenkov本质上我想删除所有字符串数据,除了每个“所有用户配置文件”后面的数据,然后取下剩余的每个字符串并将其拆分为一个列表。(我不是最擅长编程的,我还在自学,所以我为自己听起来像个傻瓜而道歉)@NateBarbettini为我冗长的帖子感到抱歉lol我是一名职业作家,所以我倾向于过度陈述自己。谢谢你的编辑,它太完美了。当我回到我的电脑工作时,我会把它发挥出来。我记得我在学习的时候就知道了这一点,但我完全忘记了!哈哈,我必须回到那个模块重新学习。如果这起作用,我将重新发布代码,并欠你50 IRL业力点。哈哈,谢谢你,因为这是在上面的小提琴,它真的不需要在一个单独的答案。网页设计规则第一,任何时候用户不必离开网站,使之成为可能。哈哈,我只是想给未来的用户带来方便。
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class Program
{
public static void Main()
{
    // output would be set by earlier code
    var output = @"Profiles on interface Wi-Fi:

Group policy profiles (read only)
---------------------------------
   <None>

  User profiles
  -------------
            All User Profile     : GuestFSN
            All User Profile     : CorporateWifi
            All User Profile     : ATT3122";

    var regex = new Regex(@"All User Profile[\s]+: (.*)");
    var resultList = new List<string>();

    foreach (Match match in regex.Matches(output))
    {
        resultList.Add(match.Groups[1].ToString());
    }

    Console.WriteLine(string.Join(", ", resultList));
}
}