主要是出于好奇,我用Ubuntu/WSL和Xming窗口服务器写了一个在Windows上启动Terminator shell的小应用。
从shell中手动操作,我可以在Windows上运行Firefox,gedit,Terminator等,非常酷。
所以我用where bash
检查了bash.exe
的位置,它返回...
C:\Windows\System32\bash.exe
然而,当我尝试运行这段代码时...
using (var xminProc = new Process())
{
xminProc.StartInfo.FileName = @"C:\Program Files (x86)\Xming\Xming.exe";
xminProc.StartInfo.Arguments = ":0 -clipboard -multiwindow";
xminProc.StartInfo.CreateNoWindow = true;
xminProc.Start();
}
using (var bashProc = new Process())
{
bashProc.StartInfo.FileName = @"C:\Windows\System32\bash.exe";
bashProc.StartInfo.Arguments = "-c \"export DISPLAY=:0; terminator; \"";
bashProc.StartInfo.CreateNoWindow = true;
bashProc.Start();
}
我得到了错误...
System.ComponentModel.Win32Exception: 'The system cannot find the file specified'
检查我的整个系统的bash.exe
显示它真的完全在另一个地方…
我不确定这个位置是否是我可以依赖的位置,我担心它是短暂的,可能会在Windows应用商店更新期间发生变化,尽管我可能在这方面是错误的。
为什么命令提示符显示bash.exe
在System32
中,但实际上它完全在另一个位置?
我可以让C#也使用System32
位置吗?
发布于 2019-05-16 08:32:33
正如@Biswapriyo所说的,首先在您的解决方案中将platafrom设置为x64:
然后你可以从c#在你的ubuntu机器上运行:
Console.WriteLine("Enter command to execute on your Ubuntu GNU/Linux");
var commandToExecute = Console.ReadLine();
// if command is null use 'ifconfig' for demo purposes
if (string.IsNullOrWhiteSpace(commandToExecute))
{
commandToExecute = "ifconfig";
}
// Execute wsl command:
using (var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = @"cmd.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
CreateNoWindow = true,
}
})
{
proc.Start();
proc.StandardInput.WriteLine("wsl " + commandToExecute);
System.Threading.Thread.Sleep(500); // give some time for command to execute
proc.StandardInput.Flush();
proc.StandardInput.Close();
proc.WaitForExit(5000); // wait up to 5 seconds for command to execute
Console.WriteLine(proc.StandardOutput.ReadToEnd());
Console.ReadLine();
}
https://stackoverflow.com/questions/49627826
复制相似问题