Start a second copy of the program with the same arguments

What is the correct way for an application to restart another copy of itself with the same arguments?

My current method is as follows:

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

However, it seems that there is a lot of busy work. Ignoring the vshost alternation, I still need to wrap the arguments with spaces with " and combine the array of arguments into one line.

Is there a better way to start a new copy of the program (including the same arguments), maybe you just need to go into Enviroment.CommandLine or accept a string array for the arguments?

+6
source share
3 answers

You will have to specify command line arguments containing spaces (and possibly other characters that I'm not sure about). Maybe something like this:

 var commandLine = string.Join(" ", args.Select(s => s.Contains(' ') ? "\"" + s + "\"" : s)); var newProcess = Process.Start("yourapplication.exe", commandLine); 

In addition, instead of using

 string[] args = Environment.GetCommandLineArgs(); 

Instead, you can simply accept them in your Main method:

 public static void Main(string[] args) { var commandLine = string.Join(" ", args.Select(s => s.Contains(' ') ? "\"" + s + "\"" : s)); var newProcess = Process.Start(Environment.GetCommandLineArgs()[0], commandLine); } 

The vshost workaround you have now seems fine, otherwise you can disable the whole vshost thing by unchecking the "Enable Visual Studio Hosting Process" checkbox on the debug tab of your project. Some debugging features are disabled when you do this. Here is a good explanation for this.

EDIT

The best way around this is to get the codebase for building the entry point:

 public static void Main(string[] args) { var imagePath = Assembly.GetEntryAssembly().CodeBase; var commandLine = string.Join(" ", args.Select(s => s.Contains(' ') ? "\"" + s + "\"" : s)); var newProcess = Process.Start(imagePath, commandLine); } 

This will work with or without vshost enabled.

+3
source

Well, that should work, I think.

 [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern System.IntPtr GetCommandLine(); static void Main(string[] args) { System.IntPtr ptr = GetCommandLine(); string commandLine = Marshal.PtrToStringAuto(ptr); string arguments = commandLine.Substring(commandLine.IndexOf("\"", 1) + 2); Console.WriteLine(arguments); Process.Start(Assembly.GetEntryAssembly().Location, arguments); } 

Link: http://pinvoke.net/default.aspx/kernel32/GetCommandLine.html

+1
source

is this enough?

 static void Main(string[] args) { string commandLineArgs = args.Join(" "); } 
-1
source

Source: https://habr.com/ru/post/917215/


All Articles