如何运行一个不知道参数从哪里开始的程序?

发布于 2024-07-07 18:50:10 字数 227 浏览 8 评论 0原文

这个主题并没有说太多,因为很难用一句话来提出问题。 我必须执行一些从注册表中读取的程序。 我必须从有人保存整个路径和参数的字段中读取内容。
我一直在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾铮苏瑾 2024-07-14 18:50:10

我已经用与上面的海报相同的方式解决了这个问题,使用 cmd.exe 和进程启动信息。

Process myProcess = New Process;
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "/C " + cmd;
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.CreateNoWindow = True;
myProcess.Start();
myProcess.WaitForExit();
myProcess.Close();

cmd /c 执行命令,然后终止。
如果进程运行时间过长,WaitForExit 将终止该进程。

I have tackled this the same way as the poster above, using cmd.exe with process start info.

Process myProcess = New Process;
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "/C " + cmd;
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.CreateNoWindow = True;
myProcess.Start();
myProcess.WaitForExit();
myProcess.Close();

cmd /c carries out the command, and then terminates.
WaitForExit will terminate the process if it runs for too long.

南笙 2024-07-14 18:50:10

事实上有几个。

  1. 您可以使用 /C [您的命令行] 作为参数来调用 cmd.exe。 这会导致 cmd.exe 处理您的命令,然后退出。
  2. 您可以将该命令写入批处理文件并启动它。

当然,还有您现在正在采取的方法,即解析命令行。

There are several, in fact.

  1. You can call cmd.exe with /C [your command line] as the arguments. This causes cmd.exe to process your command, and then quit.
  2. You could write the command to a batch file and launch that.

And of course there's the approach you're taking now, namely parsing the command line.

和影子一齐双人舞 2024-07-14 18:50:10

当未设置“UseShellExecute”时, System.Diagnostics.Process 调用 CreateProcessCreateProcessAsUser 实际启动一个程序(如果您指定用户/域/密码)。 这两个调用都可以将命令和文件作为单个参数。 来自 MSDN:

lpApplicationName 参数可以是
无效的。 在这种情况下,模块名称
一定是第一个白
中的空格分隔的标记
lpCommandLine 字符串。 ...

lpApplication 名称映射到 ProcessStartInfo.Filename,lpCommandLine 映射到 Arguments。 所以你应该可以直接去:

var processStartInfo = new ProcessStartInfo()
{
    UseShellExecute = false,
    Arguments = cmd
};
Process.Start(processStartInfo);

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:

The lpApplicationName parameter can be
NULL. In that case, the module name
must be the first white
space–delimited token in the
lpCommandLine string. ...

lpApplication name maps to ProcessStartInfo.Filename and lpCommandLine maps to Arguments. So you should be albe to just go:

var processStartInfo = new ProcessStartInfo()
{
    UseShellExecute = false,
    Arguments = cmd
};
Process.Start(processStartInfo);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文