2011-09-30 155 views
8

這裏是我的代碼:以管理員身份運行cmd以及命令?

try 
{ 
    ProcessStartInfo procStartInfo = new ProcessStartInfo(
              "cmd.exe", 
              "/c " + command); 
    procStartInfo.UseShellExecute = true; 
    procStartInfo.CreateNoWindow = true; 
    procStartInfo.Verb = "runas"; 
    procStartInfo.Arguments = "/env /user:" + "Administrator" + " cmd" + command; 

    ///command contains the command to be executed in cmd 
    System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
    proc.StartInfo = procStartInfo; 
    proc.Start(); 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.Message); 
} 

我想保持

procStartInfo.UseShellExecute = true 
procStartInfo.RedirectStandardInput = false; 

是否有可能不使用process.standardinput執行命令? 我嘗試執行我在參數中傳遞的命令,但命令不執行。

+0

**是否有可能不使用process.standardinput執行命令?我嘗試執行我在參數中傳遞的命令,但命令沒有執行。**您目前沒有使用它,所以這個問題沒有太大意義。請做你的用戶的青睞,並讓命令提示符可見,以便他們知道發生了什麼。 –

+1

你不想'動詞'。這並不意味着你的想法。我認爲。 –

+0

@Ramhound procStartInfo.Arguments =「/ env/user:」+「Administrator」+「cmd」+ ** command **; – purvang

回答

1

你爲什麼用參數初始化過程對象,然後重寫這些參數?和順便說一句:最後一位你設置參數你連接'命令'直到'cmd',這沒有多大意義,可能是它失敗的地方(看起來像你缺少一個空間)。

此外,您目前正在使用標準命令行,您可能需要使用the runas tool來代替。你也可以從命令行調用runas。

另外,你爲什麼要從命令行運行'命令'?爲什麼不直接從Process.Start直接啓動它,然後在那裏提供管理權限?這裏有點僞代碼:

Process p = Process.Start(new ProcessStartInfo() 
{ 
    FileName = <your executable>, 
    Arguments = <any arguments>, 
    UserName = "Administrator", 
    Password = <password>, 
    UseShellExecute = false, 
    WorkingDirectory = <directory of your executable> 
}); 
+0

感謝您的想法,但我不知道如何授予管理員權限process.start.can你給一些示例代碼或示例? – purvang

+0

用一些僞代碼更新了答案 – mtijn

5

正如@mtijn說,你已經得到了很多事情,你以後也覆蓋。你還需要確保你正確地逃避了事情。

比方說,你想運行高架下面的命令:

dir c:\ 

首先,如果你只是跑通過Process.Start()此命令窗口將彈出打開和關閉的時候了,因爲沒有什麼保持窗口打開。它處理命令並退出。爲了保持窗口打開,我們可以包裝在單獨的命令窗口中的命令,並使用/K開關以保持其運行:

cmd /K "dir c:\" 

要運行該命令提升我們可以使用runas.exe就像你,不同的是,我們需要轉義事情多一點。根據幫助文檔(runas /?),我們傳遞給runas的命令中的任何引號需要使用反斜線進行轉義。不幸的是,用上面的命令做了這個,給了我們一個雙反斜槓,讓cmd解析器混淆了,因此也需要轉義。所以上面的命令,都會變成:

cmd /K \"dir c:\\\" 

最後,使用你提供的,我們可以換一切成一個runas命令,並在另外一組引號括我們上面命令的語法:

runas /env /user:Administrator "cmd /K \"dir c:\\\"" 

從命令提示符運行上述命令以確保其按預期工作。

鑑於這一切最終的代碼變得更容易組裝:

 //Assuming that we want to run the following command: 
     //dir c:\ 

     //The command that we want to run 
     string subCommand = @"dir"; 

     //The arguments to the command that we want to run 
     string subCommandArgs = @"c:\"; 

     //I am wrapping everything in a CMD /K command so that I can see the output and so that it stays up after executing 
     //Note: arguments in the sub command need to have their backslashes escaped which is taken care of below 
     string subCommandFinal = @"cmd /K \""" + subCommand.Replace(@"\", @"\\") + " " + subCommandArgs.Replace(@"\", @"\\") + @"\"""; 

     //Run the runas command directly 
     ProcessStartInfo procStartInfo = new ProcessStartInfo("runas.exe"); 
     procStartInfo.UseShellExecute = true; 
     procStartInfo.CreateNoWindow = true; 

     //Create our arguments 
     string finalArgs = @"/env /user:Administrator """ + subCommandFinal + @""""; 
     procStartInfo.Arguments = finalArgs; 

     //command contains the command to be executed in cmd 
     using (System.Diagnostics.Process proc = new System.Diagnostics.Process()) 
     { 
      proc.StartInfo = procStartInfo; 
      proc.Start(); 
     } 
+0

我確實運行了你在cmd runas/env/user中建議的以下命令:Administrator「cmd/K」dir c:\\\「」即使輸入密碼,它給出了下面的輸出無法運行cmd/k「dir c:\」 – purvang

+0

您運行了'runas/env/user:Administrator「cmd/K \」dir c:\\\「」或'cmd runas/env/user:Administrator「cmd/K \」dir c:\\\「」''?首先是你應該運行的。 –

+0

我運行它爲runas/env/user:Administrator「cmd/K \」dir c:\\\「」但它給出錯誤,如下所示無法運行cmd/k「dir c:\」 – purvang

相關問題