如何运行一个不知道参数从哪里开始的程序?
这个主题并没有说太多,因为很难用一句话来提出问题。 我必须执行一些从注册表中读取的程序。 我必须从有人保存整个路径和参数的字段中读取内容。
我一直在使用 System.Diagnostics.ProcessStartInfo 设置程序的名称及其参数,但我发现了各种各样的参数,我必须解析这些参数才能将进程可执行文件保存在一个字段中,并将其参数保存在另一个字段中。
有没有办法按原样执行整个字符串?
The subject doesn't say much cause it is not easy to question in one line.
I have to execute a few programs which I read from the registry. I have to read from a field where somebody saves the whole paths and arguments.
I've been using System.Diagnostics.ProcessStartInfo setting the name of the program and its arguments but I've found a wide variety of arguments which I have to parse to save the process executable file in one field and its arguments in the other.
Is there a way to just execute the whole string as is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经用与上面的海报相同的方式解决了这个问题,使用 cmd.exe 和进程启动信息。
cmd /c 执行命令,然后终止。
如果进程运行时间过长,WaitForExit 将终止该进程。
I have tackled this the same way as the poster above, using cmd.exe with process start info.
cmd /c carries out the command, and then terminates.
WaitForExit will terminate the process if it runs for too long.
事实上有几个。
当然,还有您现在正在采取的方法,即解析命令行。
There are several, in fact.
And of course there's the approach you're taking now, namely parsing the command line.
当未设置“UseShellExecute”时, System.Diagnostics.Process 调用 CreateProcess 或CreateProcessAsUser 实际启动一个程序(如果您指定用户/域/密码)。 这两个调用都可以将命令和文件作为单个参数。 来自 MSDN:
lpApplication 名称映射到 ProcessStartInfo.Filename,lpCommandLine 映射到 Arguments。 所以你应该可以直接去:
When 'UseShellExecute' is not set, System.Diagnostics.Process calls either CreateProcess or CreateProcessAsUser to actually start a program (it uses the second one if you specify a user/domain/password). And both of those calls can take the command and file as a single argument. From MSDN:
lpApplication name maps to ProcessStartInfo.Filename and lpCommandLine maps to Arguments. So you should be albe to just go: