使用系统进程时找不到命令
Command not found when using system process
当我使用 Terminal/Bash 调用 Image Magick 命令(例如 "convert" 时,我的命令成功了。如果我使用 C# 脚本中的系统进程使用相同的命令 "convert" 将参数传递给 Bash 控制台,它将 return 并显示通过 StandardRedirectError 找不到的错误命令。
为什么使用系统进程找不到命令?例如
ProcessStartInfo startInfo = new ProcessStartInfo("/bin/bash");
startInfo.WorkingDirectory = installFolder;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
if (process != null) {
process.StandardInput.WriteLine ("convert"); //error: command not found
process.StandardInput.WriteLine ("echo \"hello world\""); //output: "hello world"
谢谢
请 post 一个更完整的代码示例,您需要告诉 Process 什么可执行文件 运行 并传入参数。 See the MSDN docs and Sample.
类似
Process process = new Process();
startInfo.Arguments = "image_[0-9].gif image_[1-9][0-9].gif animation.gif"
process.StartInfo = startInfo;
process.Start("convert.exe");
正如@MurrayFoxcroft 在正确方向上的亲切指示,这是由于非登录 shell 模式,这意味着我的 .bash_profile 未加载。这样做:
process.StandardInput.WriteLine (". ~/.bash_profile");
process.StandardInput.WriteLine ("convert");
当我使用 Terminal/Bash 调用 Image Magick 命令(例如 "convert" 时,我的命令成功了。如果我使用 C# 脚本中的系统进程使用相同的命令 "convert" 将参数传递给 Bash 控制台,它将 return 并显示通过 StandardRedirectError 找不到的错误命令。
为什么使用系统进程找不到命令?例如
ProcessStartInfo startInfo = new ProcessStartInfo("/bin/bash");
startInfo.WorkingDirectory = installFolder;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
if (process != null) {
process.StandardInput.WriteLine ("convert"); //error: command not found
process.StandardInput.WriteLine ("echo \"hello world\""); //output: "hello world"
谢谢
请 post 一个更完整的代码示例,您需要告诉 Process 什么可执行文件 运行 并传入参数。 See the MSDN docs and Sample.
类似
Process process = new Process();
startInfo.Arguments = "image_[0-9].gif image_[1-9][0-9].gif animation.gif"
process.StartInfo = startInfo;
process.Start("convert.exe");
正如@MurrayFoxcroft 在正确方向上的亲切指示,这是由于非登录 shell 模式,这意味着我的 .bash_profile 未加载。这样做:
process.StandardInput.WriteLine (". ~/.bash_profile");
process.StandardInput.WriteLine ("convert");