向ProcessStartInfo添加其他参数
本文关键字:参数 其他 添加 ProcessStartInfo | 更新日期: 2023-09-27 17:53:55
我创建了一个方法,将.exe
文件作为Admin执行。
我想对两个不同的.exe
文件使用相同的方法,但.exe
文件看起来彼此不同。因此,它们需要不同数量的参数。
方法如下:
public static int RunProcessAsAdmin(string exeName, string parameters)
{
try {
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = CurrentDirectory;
startInfo.FileName = Path.Combine(CurrentDirectory, exeName);
startInfo.Verb = "runas";
if (parameters.Contains("myValue")) {
startInfo.Arguments = parameters + "otherParam1" + "otherParam2";
} else {
startInfo.Arguments = parameters;
}
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.ErrorDialog = true;
Process process = process.Start(startInfo);
process.WaitForExit();
return process.ExitCode;
}
} catch (Exception ex) {
WriteLog(ex);
return ErrorReturnInteger;
}
}
这里if (parameters.Contains("myValue"))
i以某种方式检测正在执行的.exe
文件。但是,像这样添加参数不能正常工作:startInfo.Arguments = parameters + "otherParam1" + "otherParam2";
是否可以添加这样的附加参数?
ProcessStartInfo.Arguments
只是一个字符串,所以在每个参数之间加一个空格:
startInfo.Arguments = "argument1 argument2";
更新:
所以改变:
startInfo.Arguments = parameters + "otherParam1" + "otherParam2";
到这个(仅当您将"otherParam1"
和"otherParam2"
更改为变量时):
startInfo.Arguments = parameters + " " + "otherParam1" + " " + "otherParam2";
,如果你不打算将"otherParam1"
和"otherParam2"
更改为变量,则使用:
startInfo.Arguments = parameters + " " + "otherParam1 otherParam2";
Arguments是一个字符串,所以你可以使用string.Format
:
startInfo.Arguments = string.Format("{0} {1} {2}", parameters, otherParam1, otherParam2);