2012-11-21 37 views
2

我有一個用戶,我們稱它爲「MyUser」。它有一個密碼,假設它是「密碼」。這個用戶擁有一個git的SSH密鑰。我嘗試從我的ASP.NET應用程序運行一個發出git命令的批處理文件,它位於作爲參數傳遞的位置。我的功能如下:Git從不同用戶的批文件中提取

private void ExecuteCommand(string path, int timeout) 
    { 
     Process process = new Process(); 

     process.StartInfo = new ProcessStartInfo(); 
     process.StartInfo.FileName = "cmd.exe"; 
     process.StartInfo.Arguments = "\"" + path + "\""; 
     process.StartInfo.CreateNoWindow = true; 
     process.StartInfo.UseShellExecute = false; 
     //processInfo.WorkingDirectory = Config.GitHubRepoPath; 
     process.StartInfo.UserName = "MyUser"; 
     process.StartInfo.Password = new System.Security.SecureString(); 
     process.StartInfo.Password.AppendChar('P'); 
     process.StartInfo.Password.AppendChar('a'); 
     process.StartInfo.Password.AppendChar('s'); 
     process.StartInfo.Password.AppendChar('s'); 
     process.StartInfo.Password.AppendChar('w'); 
     process.StartInfo.Password.AppendChar('o'); 
     process.StartInfo.Password.AppendChar('r'); 
     process.StartInfo.Password.AppendChar('d'); 
     // *** Redirect the output *** 
     process.StartInfo.RedirectStandardError = true; 
     process.StartInfo.RedirectStandardOutput = true; 

     process.Start(); 

     // *** Read the streams *** 
     string output = process.StandardOutput.ReadToEnd(); 
     string error = process.StandardError.ReadToEnd(); 

     if (timeout <= 0) 
     { 
      process.WaitForExit(); 
     } 
     else 
     { 
      process.WaitForExit(timeout); 
     } 


     int exitCode = process.ExitCode; 
     process.Close(); 
     return new ShellCommandReturn { Error = error, ExitCode = exitCode, Output = output }; 
    } 

但是當我運行這個函數時,ExitCode是-1073741502,錯誤和輸出是空的。我該如何解決這個問題?

請幫助我,我試圖解決這個字面上好幾天。

+0

你能用這段代碼成功執行任何批處理文件嗎? –

+0

是的,除非它們包含git pull。 –

+0

如果是我,我會接下來驗證批處理文件運行的帳戶是否具有適當的權限。 (但那只是我。) –

回答

0

我認爲重定向標準錯誤和標準輸出&嘗試同時使用是錯誤的。請訪問以下鏈接: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput%28v=vs.100%29.aspx

允許我複製摘錄:如果父進程調用p.StandardOutput.ReadToEnd其次p.StandardError.ReadToEnd和子進程寫入

死鎖條件結果足夠的文本來填充它的錯誤流。父進程將無限期地等待子進程關閉其StandardOutput流。子進程將無限期地等待父進程從完整的StandardError流中讀取。

另一件事是......當你調用一個cmd.exe實例時,嘗試添加一個「/ c」參數。

相關問題