You can restart your application without original command line arguments using such method:
// using System.Diagnostics;
// using System.Windows.Forms;
public static void Restart()
{
ProcessStartInfo startInfo = Process.GetCurrentProcess().StartInfo;
startInfo.FileName = Application.ExecutablePath;
var exit = typeof(Application).GetMethod("ExitInternal",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Static);
exit.Invoke(null, null);
Process.Start(startInfo);
}
Also if you need to modify command line arguments, its enough to find command line arguments using Environment.GetCommandLineArgs
method and create new command line argument string and pass it to Arguments
property of startInfo
. The first item of array which GetCommandLineArgs
returns is application executable path, so we neglect it. The below example, removes a parameter /x
from original command line if available:
var args = Environment.GetCommandLineArgs().Skip(1);
var newArgs = string.Join(" ", args.Where(x => x != @"/x").Select(x => @"""" + x + @""""));
startInfo.Arguments = newArgs;
For more information about how Application.Restart
works, take a look at Application.Restart
source code.