Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向ProcessStartInfo添加其他参数_C#_.net_Vb.net - Fatal编程技术网

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);