简体   繁体   English

执行命令,如“开始”>“在C#中运行”

[英]Execute command like Start>Run in C#

I want to create a command executor like Start > Run . 我想创建一个命令执行程序,例如Start > Run The application has a TextBox and when a user enters a command eg : notepad "C:\\test.txt" it should open notepad with this file or %PROGRAMFILES% it should open 'Programs Files' directory. 该应用程序具有TextBox并且当用户输入命令时,例如: notepad "C:\\test.txt" ,应使用该文件打开记事本,或%PROGRAMFILES%应打开'Programs Files'目录。

For %PROGRAMFILES% and other Windows variables I can use Environment.ExpandEnvironmentVariable and get their path and pass to Process.Start 对于%PROGRAMFILES%和其他Windows变量,我可以使用Environment.ExpandEnvironmentVariable并获取其路径并传递给Process.Start

and for notepad, I can split the with space and first part goes in FileName and rest of string goes in Arguments of ProcessStartInfo . 对于记事本,我可以用空格分隔,第一部分放在FileName ,其余的字符串放在ProcessStartInfo Arguments中。

But what I want to know is, how does Start > Run work ? 但是我想知道的是,“开始”>“运行”如何工作? is there something like I can execute the whole command without splitting command-line or expanding the variables ? 是否可以执行整个命令而无需拆分命令行或扩展变量? Maybe with pinvoke ? 也许与pinvoke?

To the very best of my knowledge, the run dialog is implemented with a call to ShellExecuteEx . 据我所知,运行对话框是通过调用ShellExecuteEx You can achieve the same with Process.Start and UseShellExecute . 您可以使用Process.StartUseShellExecute实现相同的UseShellExecute You do need to expand the environment variables, and split the command into filename and arguments. 您确实需要扩展环境变量,并将命令拆分为文件名和参数。 You already describe how to do that in your question, and, again to the best of my knowledge, there is no programmatic interface to the functionality of the Run dialog. 您已经在问题中描述了如何执行此操作,并且据我所知,“运行”对话框的功能没有编程界面。

However, what you can do programmatically is show the Run dialog. 但是,可以以编程方式执行的是显示“运行”对话框。 Create an instance of the Shell object requesting the IShellDispatch interface, and then call the FileRun method. 创建请求IShellDispatch接口的Shell对象的实例,然后调用FileRun方法。

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

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