【问题标题】:How to run windows bash in C#如何在 C# 中运行 Windows bash
【发布时间】:2016-10-06 17:37:50
【问题描述】:

我想在 bash(Windows 中的 Linux 子系统)中运行以下命令:

bash -c "ls"

并像这样在 C# 中使用它:

ProcessStartInfo info = new ProcessStartInfo("bash", "-c \"ls\"");
Process p = Process.Start(info);
p.WaitForExit();

但它给了我以下例外:

System.ComponentModel.Win32Exception was unhandled
  ErrorCode=-2147467259
  HResult=-2147467259
  Message=The system cannot find the file specified
  NativeErrorCode=2
  Source=System
  StackTrace:
       at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
       at System.Diagnostics.Process.Start()
       at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
       at ConsoleApplication1.Program.Main(String[] args) in c:\users\matin\documents\visual studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

编辑:

我可以成功运行一个包含我的命令的批处理文件。但我想像下面的代码一样获取输出:

ProcessStartInfo info = new ProcessStartInfo("file.bat");
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
Process p = Process.Start(info);
while (!p.HasExited)
    Console.WriteLine(p.StandardOutput.ReadToEnd()); 

但它会打印:

'bash' is not recognized as an internal or external command, operable program or batch file.

【问题讨论】:

  • 你试过new ProcessStartInfo("C:\Program Files\Git\git-bash.exe")吗?
  • 试试bash的全路径名
  • @cup 没区别
  • 如果全名有空格,需要用引号括起来。
  • 我们可以假设ls 是一个更复杂的命令序列的占位符吗?在bash -c 中运行它本身是毫无意义的。

标签: c# linux windows bash visual-studio


【解决方案1】:

由于 32 位应用程序的 Windows Filesystem Redirection,无法找到该文件。您必须将 .NET 应用程序编译为 x64 才能启动 C:\Windows\System32\bash.exe

如果您尝试将RedirectStandardOutput 设置为true,那么您将得到的只是E r r o r : 0 x 8 0 0 7 0 0 5 7,它对应于Win32 的ERROR_INVALID_PARAMETER

我发现的一件事是,如果您不将任何 Redirect 属性设置为 true,Bash 似乎会继承当前控制台,但是您甚至无法在启动的 .NET 程序上重定向标准输出bash...看来Windows目前不支持将Linux子系统应用程序的输出重定向到Win32应用程序。

【讨论】:

  • 这就是我的情况 - 我确实以 Any CPU 作为目标编译了我的应用程序。切换到 x64 后,找不到文件的问题消失了。 E r r o r : 0 x 8 0 0 7 0 0 5 7 问题是一个已知的 WSL 问题 (github.com/Microsoft/BashOnWindows/issues/2),并且已在内部版本中修复。
【解决方案2】:

一种解决方法是在下面创建批处理文件:

bash -c "ls > log.txt"

并使用下面的 C# 程序:

ProcessStartInfo info = new ProcessStartInfo("file.bat");
Process p = Process.Start(info);
p.WaitForExit();
Console.WriteLine(File.ReadAllText("log.txt"));

输出将写入log.txt 文件。

但它有一些问题:

  1. 它会打开一个 cmd 窗口以运行批处理文件。
  2. 在进程退出之前,您无法读取输出。
  3. 您不能重定向标准输入

所以问题还没有解决。

【讨论】:

    【解决方案3】:

    如果我理解你的问题,试试这个,这可能会有所帮助。

    string strCmdText;
    strCmdText= "bash -c \"time\"";
    System.Diagnostics.Process.Start("CMD.exe",strCmdText);
    

    【讨论】:

      猜你喜欢
      • 2020-09-15
      • 2015-02-22
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 2017-03-29
      • 2019-08-19
      • 2017-11-28
      相关资源
      最近更新 更多