C# 如何在C中运行WindowsBash#

C# 如何在C中运行WindowsBash#,c#,linux,windows,bash,visual-studio,C#,Linux,Windows,Bash,Visual Studio,我想在bash(Windows中的Linux子系统)中运行以下命令: 在C#中使用它,如下所示: ProcessStartInfo info = new ProcessStartInfo("bash", "-c \"ls\""); Process p = Process.Start(info); p.WaitForExit(); 但它给了我以下的例外: System.ComponentModel.Win32Exception was unhandled ErrorCode=-2147467

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

在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.

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

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

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

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
文件

但它也有一些问题:

  • 它打开一个cmd窗口以运行批处理文件
  • 在进程退出之前,无法读取输出
  • 无法重定向标准输入

  • 因此,问题尚未解决。

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

    但是,如果您尝试将
    RedirectStandardOutput
    设置为
    true
    ,您将得到的只是与Win32的
    ERROR\u INVALID\u参数相对应的
    ERROR:0 x 8 0 0 7 0 0 5 7


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

    您是否尝试过
    新的ProcessStartInfo(“C:\Program Files\Git\Git bash.exe”)
    ?尝试bash@cup没有区别如果全名中有空格,名称需要用引号括起来。我们可以假定
    ls
    是更复杂的命令序列的占位符吗?在
    bash-c
    中运行它本身是毫无意义的。这就是我的情况——我确实以任何CPU为目标编译了我的应用程序。切换到x64后,文件未找到问题消失。这是一个已知的WSL问题(),已在内部构建中修复。
    ProcessStartInfo info = new ProcessStartInfo("file.bat");
    Process p = Process.Start(info);
    p.WaitForExit();
    Console.WriteLine(File.ReadAllText("log.txt"));