Command netsh using C#

情到浓时终转凉″ 提交于 2019-12-04 02:35:31

问题


I want to create a C# application to create a WLAN network. I currently use netsh using command prompt. My application should do this in button click. Here is the command I use in command prompt in admin mode "netsh wlan set hostednetwork mode=allow ssid=sha key=12345678" after that I enter "netsh wlan start hostednetwork". When I do this i can create a wifi local area network. In C# I coded like below

private void button1_Click(object sender, EventArgs e)
{
     Process p = new Process();
     p.StartInfo.FileName = "netsh.exe";
     p.StartInfo.Arguments = "wlan set hostednetwork mode=allow ssid=sha key=12345678"+"netsh wlan start hostednetwork";            
     p.StartInfo.UseShellExecute = false;
     p.StartInfo.RedirectStandardOutput = true;
     p.Start();                       
}

回答1:


You shouldn't do this: +"netsh wlan start hostednetwork" to the arguments of the first process. That would mean that you are typing this at the console:

netsh wlan set hostednetwork mode=allow ssid=sha key=12345678netsh wlan start hostednetwork

Instead, make a new process for the second line:

private void button1_Click(object sender, EventArgs e)
{
     Process p1 = new Process();
     p1.StartInfo.FileName = "netsh.exe";
     p1.StartInfo.Arguments = "wlan set hostednetwork mode=allow ssid=sha key=12345678";            
     p1.StartInfo.UseShellExecute = false;
     p1.StartInfo.RedirectStandardOutput = true;
     p1.Start();

     Process p2 = new Process();
     p2.StartInfo.FileName = "netsh.exe";
     p2.StartInfo.Arguments = "wlan start hostednetwork";            
     p2.StartInfo.UseShellExecute = false;
     p2.StartInfo.RedirectStandardOutput = true;
     p2.Start();
}


来源:https://stackoverflow.com/questions/18400364/command-netsh-using-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!