gpt4 book ai didi

c# - Git 从不同用户的批处理文件中提取

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:24 24 4
gpt4 key购买 nike

我有一个用户,我们称它为“MyUser”。它有一个密码,假设它是“Password”。这个用户有一个 git 的 SSH key 。我尝试从我的 ASP.NET 应用程序运行一个发出 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 并且错误和输出都是空的。我该如何解决此问题?

请帮帮我,我已经尝试从字面上解决这个问题好几天了。

最佳答案

我认为重定向标准错误和标准输出并尝试同步使用两者是错误的。请看这个链接: 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”参数。

关于c# - Git 从不同用户的批处理文件中提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13495505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com