Restart an application by itself

后端 未结 10 643
醉话见心
醉话见心 2020-12-13 02:46

I want to build my application with the function to restart itself. I found on codeproject

ProcessStartInfo Info=new ProcessStartInfo();
Info.Arguments=\"/C          


        
10条回答
  •  醉话见心
    2020-12-13 03:32

    I use similar code to the code you tried when restarting apps. I send a timed cmd command to restart the app for me like this:

    ProcessStartInfo Info = new ProcessStartInfo();
    Info.Arguments = "/C ping 127.0.0.1 -n 2 && \"" + Application.ExecutablePath + "\"";
    Info.WindowStyle = ProcessWindowStyle.Hidden;
    Info.CreateNoWindow = true;
    Info.FileName = "cmd.exe";
    Process.Start(Info);
    Application.Exit(); 
    

    The command is sent to the OS, the ping pauses the script for 2-3 seconds, by which time the application has exited from Application.Exit(), then the next command after the ping starts it again.

    Note: The \" puts quotes around the path, incase it has spaces, which cmd can't process without quotes.

    Hope this helps!

提交回复
热议问题