我有以下代码来启动cmd提示符,然后从该cmd提示符下启动putty.exe,并在末尾输入IP地址。
private void btnRouter_Click(Object sender, EventArgs e)
{
MessageBox.Show((string)((Button)sender).Tag);
System.Diagnostics.Process cmd = new System.Diagnostics.Process();
cmd.StartInfo.FileName = @"C:\windows\system32\cmd.exe";
cmd.StartInfo.UseShellExecute = true;
cmd.Start();
cmd.StandardInput.WriteLine("Putty.exe " + ((string)((Button)sender).Tag));
cmd.WaitForExit();
}
问题是我不断收到错误消息“ StandardIn尚未重定向”。从Visual Studio中获取,当我尝试在启动的命令窗口中键入putty.exe时,我得到“'putty.exe'无法识别为内部或外部命令,可操作程序或批处理文件”。这真的很奇怪,因为如果我进入运行行,请键入cmd然后键入putty.exe,因为我将绝对文件夹路径添加到腻子应用程序的系统环境路径中后,它立即打开。
从Visual Studio打开的CMD不使用我的环境路径是有原因的吗?
仍然不知道为什么会发生这种情况,但是我回到了以前的代码,将putty.exe的副本放到了调试文件夹中,这次它成功启动了。
private void btnRouter_Click(Object sender, EventArgs e)
{
MessageBox.Show((string)((Button)sender).Tag);
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = "Putty.exe ";
startInfo.Arguments = ((string)((Button)sender).Tag);
myProcess.StartInfo = startInfo;
myProcess.Start();
}
此行将导致“ StandardIn尚未重定向”。错误,因为您尝试写入stdin且该句柄未正确设置为输入:
cmd.StandardInput.WriteLine("Putty.exe " + ((string)((Button)sender).Tag));
关于这个问题:
从Visual Studio打开的CMD不使用我的环境路径是有原因的吗?
当父进程启动子进程时,该子进程将继承其父级的环境。
换句话说,您正在启动的新cmd窗口将继承Visual Studio环境,但这并不意味着Visual Studio环境与命令提示符的环境相同。
您可以通过以下方法进行测试:启动命令行提示符,从该命令行提示符运行Visual Studio,然后创建您的子cmd进程。
现在,您的cmd进程应具有与原始命令行匹配的环境,以及Visual Studio添加到其环境副本的所有更改。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句