Application.Restart not passing arguments back

筅森魡賤 提交于 2019-12-07 02:01:10

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/8767325/application-restart-not-passing-arguments-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!