应用程序使用相同参数重新启动自身的另一个副本的正确方法是什么?
我目前的方法是执行以下操作
static void Main()
{
Console.WriteLine("Start New Copy");
Console.ReadLine();
string[] args = Environment.GetCommandLineArgs();
//Will not work if using vshost, uncomment the next line to fix that issue.
//args[0] = Regex.Replace(args[0], "\\.vshost\\.exe$", ".exe");
//Put quotes around arguments that contain spaces.
for (int i = 1; i < args.Length; i++)
{
if (args[i].Contains(' '))
args[i] = String.Concat('"', args[i], '"');
}
//Combine the arguments in to one string
string joinedArgs = string.Empty;
if (args.Length > 1)
joinedArgs = string.Join(" ", args, 1, args.Length - 1);
//Start the new process
Process.Start(args[0], joinedArgs);
}
然而,那里似乎有很多忙碌的工作。忽略 vshost 的条带化,我仍然需要将带有空格"
的参数包装起来,并将参数数组组合成一个字符串。
有没有更好的方法来启动程序的新副本(包括相同的参数),也许只需要传入Enviroment.CommandLine或使用字符串数组作为参数?