Netsh.exe运行行

时间:2015-03-03 11:12:45

标签: c# netsh

 System.Diagnostics.ProcessStartInfo netshprocess = new System.Diagnostics.ProcessStartInfo();
 netshprocess.FileName = "netsh.exe";
 string temp = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
 string user = "";
 for (int i = 0; i < temp.Length; i++)
 {
     user += temp[i];
     if (temp[i] == '\\')
     {
         user = "";
     }
  }
  netshprocess.Arguments = "";
  netshprocess.Arguments = "http add urlacl url=http://+:7015/MeasurementTransferWCF user=" + user;
  netshprocess.UseShellExecute = true;
  System.Diagnostics.Process.Start(netshprocess).WaitForExit();

当我打开netsh.exe并手动输入代码对其工作的参数所具有的确切字符串时,我似乎无法通过代码运行它(测试然后删除了添加,因此可以尝试执行此操作在代码中)。我也试过以管理员模式运行,但它不起作用。我错过了什么让它运行?

没有错误它只是在调用process.start时没有运行争论

1 个答案:

答案 0 :(得分:2)

 System.Diagnostics.ProcessStartInfo netshprocess = new System.Diagnostics.ProcessStartInfo();
 netshprocess.FileName = "netsh.exe";


 string temp = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
 string user = "";
 for (int i = 0; i < temp.Length; i++)
 {
     user += temp[i];
     if (temp[i] == '\\')
     {
         user = "";
     }
 }
 netshprocess.Verb = "runas";
 netshprocess.Arguments = "";
 netshprocess.Arguments = "http add urlacl url=http://+:7015/MeasurementTransferWCF user=" + user;
 netshprocess.UseShellExecute = true;
 try
 {

     System.Diagnostics.Process.Start(netshprocess).WaitForExit();
 }
 catch (Exception)

 { }

使用动词运行时,用户单击是,它将添加它,但如果他们单击否,您知道需要捕获异常,因为它将git无法运行错误