C# 向ProcessStartInfo添加其他参数
我创建了一个方法,该方法将以管理员身份执行C# 向ProcessStartInfo添加其他参数,c#,.net,vb.net,C#,.net,Vb.net,我创建了一个方法,该方法将以管理员身份执行.exe文件。 我想对两个不同的.exe文件使用相同的方法,但是.exe文件看起来彼此不同。因此,它们需要不同数量的参数。 方法如下: public static int RunProcessAsAdmin(string exeName, string parameters) { try { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo
.exe
文件。我想对两个不同的
.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”)
我不知何故检测到正在执行哪个.exe
文件。但是,像这样添加参数并不能正常工作:startInfo.Arguments=parameters+“otherParam1”+“otherParam2”代码>
是否可以添加这样的附加参数 ProcessStartInfo.Arguments
只是一个字符串,所以在每个参数之间留出一个空格:
startInfo.Arguments = "argument1 argument2";
更新:
因此,改变:
startInfo.Arguments = parameters + "otherParam1" + "otherParam2";
为此(仅当您将“otherParam1”
和“otherParam2”
更改为变量时):
如果您不打算将“otherParam1”
和“otherParam2”
更改为变量,则使用:
startInfo.Arguments = parameters + " " + "otherParam1 otherParam2";
参数是字符串,因此可以使用string.Format
:
startInfo.Arguments = string.Format("{0} {1} {2}", parameters, otherParam1, otherParam2);
记住在参数之间添加空格。
startInfo.Arguments = string.Format("{0} {1} {2}", parameters, otherParam1, otherParam2);