Question

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)?

Was it helpful?

Solution

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.

OTHER TIPS

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

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top