题主不多说,一句话就不好问了。我必须执行一些从注册表中读取的程序。我必须从有人保存整个路径和参数的字段中读取。
我一直在使用 System.Diagnostics.ProcessStartInfo 设置程序的名称及其参数,但我发现了各种各样的参数,我必须解析这些参数才能将进程可执行文件保存在一个字段中,并将其参数保存在另一个字段中。
有没有办法按原样执行整个字符串?
题主不多说,一句话就不好问了。我必须执行一些从注册表中读取的程序。我必须从有人保存整个路径和参数的字段中读取。
我一直在使用 System.Diagnostics.ProcessStartInfo 设置程序的名称及其参数,但我发现了各种各样的参数,我必须解析这些参数才能将进程可执行文件保存在一个字段中,并将其参数保存在另一个字段中。
有没有办法按原样执行整个字符串?
我已经以与上面的海报相同的方式解决了这个问题,使用带有进程启动信息的 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 如果运行时间过长将终止进程。
事实上,有几个。
当然还有你现在采用的方法,即解析命令行。
如果未设置“ UseShellExecute ”,则 System.Diagnostics.Process 调用CreateProcess或CreateProcessAsUser以实际启动程序(如果您指定用户/域/密码,它将使用第二个程序)。并且这两个调用都可以将命令和文件作为单个参数。来自 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);