Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application.Restart not passing arguments back

This is a ClickOnce application. According to the documentation, "If your application was originally supplied command-line options when it first executed, Restart will launch the application again with the same options.". But I don't know if this is supposed to 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 associated my application with the .abc extension and when I double click my .abc file, the application will launch with the file name as the only argument, but then when I restart (by pressing my button1), GetCommandLineFile() will return an empty array.

like image 788
Juan Avatar asked Jan 07 '12 04:01

Juan


Video Answer


1 Answers

I believe Application.Restart was designed for standard command line arguments instead of how ClickOnce applications handle it.

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

I think a better 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 up, GetCommandLineFile() should grab the arguments again.

like image 123
Issa Fram Avatar answered Oct 15 '22 03:10

Issa Fram