.NET:读取命令提示符输出

发布于 2025-01-06 07:41:38 字数 921 浏览 1 评论 0原文

我想从 .net 调用 netsh 命令,我使用 Process 类来启动调用 netsh 命令的进程,及其工作正常,现在我想获取 .NET 中的 netsh 命令返回的输出,例如我在 Process 中调用以下命令:

netsh wlan show hostednetwork

这将返回列表活跃的托管网络。

如何读取 .NET 中的列表?
任何人都可以帮助我或带我走上正确的道路(我不想使用任何第三方工具)?


更新 下面是我使用 netsh wlan show Hostednetwork 命令的返回输出

托管网络设置

Mode                   : Allowed
SSID name              : "AqaMaula(TUS)"
Max number of clients  : 100
Authentication         : WPA2-Personal
Cipher                 : CCMP

托管网络状态

Status                 : Started
BSSID                  : 06:65:9d:26:f4:b7
Radio type             : 802.11n
Channel                : 11
Number of clients      : 1
    d0:c1:b1:44:8b:f0        Authenticated

谁能告诉我如何获取所有单独数据并将它们放入数据库中,例如模式、SSID 名称等(单独)?

I want to call the netsh command from .net, I am using the Process class to initiate the process which calls the netsh command, and its working fine, now I want to get the output returned by the netsh command in .NET, for example I am calling the below command in Process:

netsh wlan show hostednetwork

this is returning me the list of active hostednetwork.

How can I read the list in .NET?
Can anyone assist me or take me to the right path (I don't want to use any third party tools)?


UPDATES
Below is my return output using netsh wlan show hostednetwork command

Hosted network settings

Mode                   : Allowed
SSID name              : "AqaMaula(TUS)"
Max number of clients  : 100
Authentication         : WPA2-Personal
Cipher                 : CCMP

Hosted network status

Status                 : Started
BSSID                  : 06:65:9d:26:f4:b7
Radio type             : 802.11n
Channel                : 11
Number of clients      : 1
    d0:c1:b1:44:8b:f0        Authenticated

Can anyone tell me how can i get all individual data and put them in database like, Mode, SSID Name, etc. (individually)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

过去的过去 2025-01-13 07:41:38

这是代码:

using System;
using System.Diagnostics;

public static class Program
{
    public static void Main()
    {
        var output = RunProcess();
        Console.WriteLine(output);
    }

    /// <summary>
    /// Runs the process: starts it and waits upon its completion.
    /// </summary>
    /// <returns>
    /// Standart output content as a string.
    /// </returns>
    private static string RunProcess()
    {
        using (var process = CreateProcess())
        {
            process.Start();

            // To avoid deadlocks, always read the output stream first and then wait.
            // For details, see: [Process.StandardOutput Property (System.Diagnostics)](https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.110).aspx).
            var output = process.StandardOutput.ReadToEnd();
            process.WaitForExit();

            return output;
        }
    }

    private static Process CreateProcess()
    {
        return new Process
            {
                StartInfo =
                    {
                        FileName = "netsh",
                        Arguments = "wlan show hostednetwork",
                        UseShellExecute = false,
                        RedirectStandardOutput = true
                    }
            };
    }
}

更新

我认为您应该使用 托管 Wifi API 框架而不是解析命令行实用程序的结果。这是更可靠的方式。看一下源代码,它们包含 WifiExample

Here is the code:

using System;
using System.Diagnostics;

public static class Program
{
    public static void Main()
    {
        var output = RunProcess();
        Console.WriteLine(output);
    }

    /// <summary>
    /// Runs the process: starts it and waits upon its completion.
    /// </summary>
    /// <returns>
    /// Standart output content as a string.
    /// </returns>
    private static string RunProcess()
    {
        using (var process = CreateProcess())
        {
            process.Start();

            // To avoid deadlocks, always read the output stream first and then wait.
            // For details, see: [Process.StandardOutput Property (System.Diagnostics)](https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.110).aspx).
            var output = process.StandardOutput.ReadToEnd();
            process.WaitForExit();

            return output;
        }
    }

    private static Process CreateProcess()
    {
        return new Process
            {
                StartInfo =
                    {
                        FileName = "netsh",
                        Arguments = "wlan show hostednetwork",
                        UseShellExecute = false,
                        RedirectStandardOutput = true
                    }
            };
    }
}

Update

I think you should use Managed Wifi API framework instead of parsing results of command line utility. It is more reliable way. Take a look at the sources, they contain WifiExample.

能怎样 2025-01-13 07:41:38

使用StandardOutput Process 类的属性。
上面链接的 MSDN 页面附带了如何使用 StandardOutput 的简单示例。

Use the StandardOutput property of the Process class.
The above linked MSDN page comes with a simple example of how to use StandardOutput.

情栀口红 2025-01-13 07:41:38

在启动进程之前,您需要将 StartInfo.UseShellExecute 设置为 false,并将 RedirectStandardOutput 设置为 true,然后从 Process.StandardOutput 读取。

所有内容均记录在 MSDN 上: http://msdn.microsoft .com/en-us/library/system.diagnostics.process.standardoutput.aspx

You need to set StartInfo.UseShellExecute to false and RedirectStandardOutput to true before starting the process, and then read from Process.StandardOutput.

All documented on MSDN here: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

弱骨蛰伏 2025-01-13 07:41:38

它应该是这样的:

//@param output The output captured from the netsh command in String format
//@param key The key you are trying to find (in your example "Mode")
public String getValue(String output, String key) {
    MatchCollection matches;
    Regex rx = new Regex(@"(?<key>[A-Za-z0-0 ]+)\t\:\s(?<value>[.]+)");
    matches = rx.Matches(output);

    foreach (Match match in matches) {
        GroupCollection groups = match.Groups;

        if (groups["key"] == key) {
            return groups["value"];
        }
    }
}

不幸的是,我无法测试它来修复任何小错误。

另外,如果您要经常引用它们,我会在解析后将它们放入字典中,以减少查找时间。

It should be something like this:

//@param output The output captured from the netsh command in String format
//@param key The key you are trying to find (in your example "Mode")
public String getValue(String output, String key) {
    MatchCollection matches;
    Regex rx = new Regex(@"(?<key>[A-Za-z0-0 ]+)\t\:\s(?<value>[.]+)");
    matches = rx.Matches(output);

    foreach (Match match in matches) {
        GroupCollection groups = match.Groups;

        if (groups["key"] == key) {
            return groups["value"];
        }
    }
}

Unfortunately, I can't test it atm to fix any small bugs.

Also, if you are going to reference them often, I'd place them into a Dictionary after parsing, to decrease lookup time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文