2016-07-01 5 views
2

.exeファイルを管理者として実行する方法を作成しました。
2つの異なる.exeファイルに対して同じ方法を使用したいと思いますが、.exeファイルはお互いに違って見えます。したがって、それらは異なる数のパラメータを必要とする。
方法は、以下のようなものです:ここではProcessStartInfoにパラメータを追加

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

このようなパラメータを追加することは可能ですか?

+0

覚えておいてください引数の間に空白を追加します。 –

答えて

4

ProcessStartInfo.Argumentsので、各引数の間にスペースを入れるだけの文字列です:

startInfo.Arguments = "argument1 argument2"; 

アップデート:

ので変更:この

startInfo.Arguments = parameters + "otherParam1" + "otherParam2"; 

を(のみ必要になります場合には変数に"otherParam1""otherParam2"を変更してください)

startInfo.Arguments = parameters + " " + "otherParam1" + " " + "otherParam2"; 

、あなたが変数に"otherParam1""otherParam2"を変更するつもりはない場合は、次に使用:あなたはstring.Formatを使用することができるように

startInfo.Arguments = parameters + " " + "otherParam1 otherParam2"; 
0

引数は文字列です:

startInfo.Arguments = string.Format("{0} {1} {2}", parameters, otherParam1, otherParam2); 
関連する問題