Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C#在Mingw上从其他进程运行命令?_C#_.net_Command Line_Process_Mingw - Fatal编程技术网

如何使用C#在Mingw上从其他进程运行命令?

如何使用C#在Mingw上从其他进程运行命令?,c#,.net,command-line,process,mingw,C#,.net,Command Line,Process,Mingw,我正试图使用以下代码在其他进程的Mingw上执行命令: ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = @"PATH-TO-MINGW\mingwenv.cmd"; startInfo.UseShellExecute = false; startInfo.RedirectStandardInput = true; using (Process exeProcess = Proce

我正试图使用以下代码在其他进程的Mingw上执行命令:

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"PATH-TO-MINGW\mingwenv.cmd";        
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;

using (Process exeProcess = Process.Start(startInfo))
{                
   StreamWriter str = exeProcess.StandardInput;
   str.WriteLine("ls");               

   exeProcess.WaitForExit();
}
但这段代码只是午餐,不输入命令。
我是否错过了某些事情或无法做到?
谢谢
更新
基于,我的解决方案是这样的(我使用OMNeT++模拟器,所以目录基于它)

你应该这样做

str.Flush();
因此,您编写的命令将传递给进程

在处理流时,还应该使用
using
语句

     using (Process exeProcess = Process.Start(startInfo))
     {
        using(StreamWriter str = exeProcess.StandardInput)
        {
           str.WriteLine("ls");
           str.Flush();

           exeProcess.WaitForExit();
        }
     }
我怀疑c#正在CMD提示符中启动您的mingw命令。您需要在bashshell中生成您的进程。尝试用“bash-l-c'ls”或“bash-c'ls”包装您的命令。确保bash在您的路径中,并确保引用命令参数(如果有)。在python中从popen生成bash命令时,我不得不使用这个方法。我知道不同的语言,但可能有关联

我想代码将类似于此。我还没有考过C#:


谢谢你的回答,我已经试过str.Flush()了,但没用。谢谢你的回答,它很管用。我用工作代码更新了我的问题。
     using (Process exeProcess = Process.Start(startInfo))
     {
        using(StreamWriter str = exeProcess.StandardInput)
        {
           str.WriteLine("ls");
           str.Flush();

           exeProcess.WaitForExit();
        }
     }
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "bash.exe";
startInfo.Arguments = "-l -c 'ls -l /your/msys/path'";
# Or other examples with windows path:
#   startInfo.Arguments = "-l -c 'ls -l /c/your/path'";
#   startInfo.Arguments = "-l -c 'ls -l C:/your/path'";
#   startInfo.Arguments = "-l -c 'ls -l C:\\your\\path'";
process.StartInfo = startInfo;
process.Start();