2015-03-03 36 views
1
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並手動輸入代碼對參數有效的確切字符串時,似乎無法通過代碼運行它(測試然後刪除了添加這樣可以嘗試在代碼中執行)。我也嘗試以管理員模式運行,但它不起作用。我錯過了什麼讓它運行?Netsh.exe運行行

它不對錯誤,它只是不運行arguement時的Process.Start被稱爲

+1

刪除'catch(Exception){}'並找出你自己。您也可能想要閱讀過程的標準和錯誤輸出。 – CodeCaster 2015-03-03 11:16:31

+0

它沒有得到cought在try catch,它不會崩潰,如果它不存在它打開netsh和關閉只是沒有運行的爭論@codecaster – 2015-03-03 11:18:49

回答

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無法運行錯誤

+0

謝謝你這完美的工作,但無論如何,只有運行動詞行如果當前程序以admin身份運行? – 2015-03-03 11:28:15