我尝试通过 C# 控制台应用程序使用一些 svn 命令开始启动外部 CMD 进程。通过该过程,我可以读取错误输出和正常输出。在某些情况下, .ReadLine() 方法会卡住。大多数情况下,当我将此命令发送到进程 'c: & cd "C:\xxxx\xxxx" & svn revert 时会发生这种情况。-R & svn update & svn upgrade & svn merge "https://svn.xxxxxxxxxxx.xx/svn/xxxx/trunk"'。我在互联网上搜索这个问题,但我找不到正确的解决方案。下面的代码用于启动进程并读取输出:
public static List<string> StartCmdCommand(bool returnOutput, bool printOutput, params string[] commands)
{
var startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = false,
Verb = "runas",
Arguments = "runas /savecred /profile /user:Admin ",
UseShellExecute = false
};
using (var process = Process.Start(startInfo))
{
foreach (var command in commands)
process.StandardInput.WriteLine(command);
process.StandardInput.Flush();
process.StandardInput.Close();
List<string> lines = new List<string>();
String line;
var standardErrorReader = process.StandardError;
while ((line = standardErrorReader.ReadLine()) != null)
{
lines.Add(line);
if (printOutput)
LogHelper.Write(line);
}
var standardOutputReader = process.StandardOutput;
while ((line = standardOutputReader.ReadLine()) != null)
{
lines.Add(line);
if (printOutput)
LogHelper.Write(line);
}
return lines;
}
return null;
}
有没有人有这个问题的经验?
先感谢您!
亲切的问候,耶勒