Application.Restart does not pass arguments back

This is a ClickOnce app. According to the documentation : "If your application was initially provided with command-line options on first start, rebooting will start the application again with the same parameters.". But I don't know if this should work or not with ClickOnce applications. If so, what am I doing wrong?

Here is my code:

public Form1() { InitializeComponent(); textBox1.Text = string.Join(Environment.NewLine, GetCommandLineFile()); } private static string[] GetCommandLineFile() { if (AppDomain.CurrentDomain != null && AppDomain.CurrentDomain.SetupInformation != null && AppDomain.CurrentDomain.SetupInformation.ActivationArguments != null && AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData != null && AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData.Any()) { return AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData; } else return new string[] { }; } private void button1_Click(object sender, EventArgs e) { Application.Restart(); } 

I linked my application with the .abc extension, and when I double-clicked my .abc file, the application will start with the file name as the only argument, but then when I restart (by pressing my button1 ), GetCommandLineFile() will return an empty array.

+6
source share
1 answer

I believe Application.Restart was designed for standard command line arguments, not as a ClickOnce application.

By looking at the Microsoft code for Application.Restart , they explicitly check to see if the application is a ClickOnce application, and then restarts it without any passed arguments. Any other application gets Environment.GetCommandLineArgs() parsed and sent to a new process.

I think the best solution, instead of writing arguments to a file, is to simply start a new process as such:

 "path\Application Name.appref-ms" arg1,arg2,arg3 

That way, when your application starts, GetCommandLineFile() will take the arguments again.

+3
source

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


All Articles