Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET: Reading the command prompt output

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

like image 275
Abbas Avatar asked Jan 17 '26 03:01

Abbas


2 Answers

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.

like image 101
Sergey Vyacheslavovich Brunov Avatar answered Jan 19 '26 18:01

Sergey Vyacheslavovich Brunov


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

like image 22
Albin Sunnanbo Avatar answered Jan 19 '26 17:01

Albin Sunnanbo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!