2014-12-03 219 views
0

我試圖通過c#執行cmd命令,一切正常。 我如何執行包含密碼的「runas」cmd,或者至少如何打開用於插入密碼的cmd提示符。我的代碼執行效果很好,這是我無法解決的唯一問題。 我試過甚至與回聲,但它似乎並沒有工作。C#執行cmd命令

private void btnStart_Click(object sender, EventArgs e) 
{ 

    txtResult.Text = string.Empty; 
    if (txtExecutable.Text.Trim() != string.Empty) 
    { 
     StreamReader outputReader = null; 
     StreamReader errorReader = null; 
     try 
     { 
      System.Diagnostics.Process process = new System.Diagnostics.Process(); 
      System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); 
      startInfo.CreateNoWindow = true; 
      startInfo.FileName = "cmd.exe"; 
      startInfo.Arguments = @"/C echo " + txtpass.Text + " | runas /user:" + utente.Text + " wmic.exe /node:" + txtExecutable.Text + " ComputerSystem Get UserName && tasklist /s " + txtExecutable.Text + @" /fi ""imagename eq EmpirumRCHost.exe"" && msinfo32.exe \\" + txtParameter.Text; 
      startInfo.ErrorDialog = false; 
      startInfo.UseShellExecute = false; 
      startInfo.RedirectStandardError = true; 
      startInfo.RedirectStandardInput = true; 
      startInfo.RedirectStandardOutput = true; 
      process.StartInfo = startInfo; 
      process.Start(); 

      bool processStarted = process.Start(); 
      if (processStarted) 
      { 
       //Get the output stream 
       outputReader = process.StandardOutput; 
       errorReader = process.StandardError; 
       process.WaitForExit(); 

       //Display the result 
       string displayText = "Output" + Environment.NewLine + "==============" + Environment.NewLine; 
       displayText += outputReader.ReadToEnd(); 
       displayText += Environment.NewLine + "Error" + Environment.NewLine + "==============" + 
           Environment.NewLine; 
       displayText += errorReader.ReadToEnd(); 
       txtResult.Text = displayText; 
      } 
     } 
     finally 
     { 
      if (outputReader != null) 
      { 
       outputReader.Close(); 
      } 
      if (errorReader != null) 
      { 
       errorReader.Close(); 
      } 
      btnStart.Enabled = true; 
     } 
    } 
} 

我需要運行runas命令作爲在遠程pc上具有管理權限的域用戶。

+0

你不能運行你的應用程序作爲管理員? 如果不是我認爲你會在這裏找到解決方案:http://stackoverflow.com/questions/8690552/run-elevated-process – Jontatas 2014-12-03 11:45:25

+0

我認爲你需要重寫你的問題,如果你的問題是關於如何管道輸入在孩子的過程的標準輸入,請嘗試http://www.codeproject.com/Articles/18577/How-to-redirect-Standard-Input-Output-of-an-applic – BlueTrin 2014-12-03 11:47:45

+0

事情是我需要runas作爲遠程計算機上的域管理員,而不是本地管理員。 – 2014-12-03 12:00:39

回答

0

您可以通過添加應用程序清單文件嘗試爲討論here

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> 
+0

事情是我需要runas作爲域管理員在遠程機器上,而不是本地管理員。 – 2014-12-03 12:01:34

0

這是所有你需要做的在C#

運行shell命令
string strCmdText; 
strCmdText= "/C copy /b Image1.jpg + Archive.rar Image2.jpg"; 
System.Diagnostics.Process.Start("CMD.exe",strCmdText);