简体   繁体   English

如何在当前cmd.exe上下文中执行Shell命令?

[英]How to execute a shell command in the current cmd.exe context?

So, I have a .Net utility. 因此,我有一个.Net实用程序。 I need to use a utility(ilasm and ildasm) that is sometimes in the PATH variable and sometimes not. 我需要使用有时在PATH变量中,有时没有的实用程序(ilasm和ildasm)。 Specific times when it's in the path includes when using the Visual Studio command line script.. and when it's not is when you just launch a regular cmd.exe instance. 它在路径中的特定时间包括使用Visual Studio命令行脚本时的时间,而不是在仅启动常规cmd.exe实例时的时间。

All of the information I've seen for executing a shell command from .Net imply that you can just create a new instance of cmd.exe, but in this case, the PATH variable will be different. 我已经看到了有关从.Net执行shell命令的所有信息,这意味着您可以仅创建cmd.exe的新实例,但是在这种情况下,PATH变量将有所不同。 The other possible thing is manually parsing the PATH variable and execute it directly with a ProcessStartInfo when found, but this seems error prone and unnecessary 另一可能的事情是手动解析PATH变量,并在找到时直接使用ProcessStartInfo直接执行它,但这似乎容易出错并且不必要

Is there a tool or anything that would help me with this situation? 是否有工具或其他工具可以帮助我解决这种情况? How should I go about executing these utilities? 我应该如何执行这些实用程序?

When you start cmd.exe, you can set its environment path: 启动cmd.exe时,可以设置其环境路径:

 var psi = new ProcessStartInfo("cmd.exe");
 psi.UseShellExecute = false;
 psi.EnvironmentVariables["path"] = "mypath";
 Process.Start(psi);

Please prefer comment before downvote. 请先发表评论,然后再投票。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM