Application.Restart没有传递参数回 [英] Application.Restart not passing arguments back

查看:202
本文介绍了Application.Restart没有传递参数回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个ClickOnce应用程序。根据<一href="http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k%28SYSTEM.WINDOWS.FORMS.APPLICATION.RESTART%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK,VERSION=V4.0%22%29;k%28DevLang-CSHARP%29&rd=true">documentation, 如果您的应用程序最初提供的命令行选项它第一次执行时,重新启动将再次启动应用程序使用相同的选项。的。但我不知道这应该是工作或不符合ClickOnce应用程序。如果是的话,我究竟哪里做错了?

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?

下面是我的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();
}

我有关我与 .abc的扩展应用程序,当我双击我的 .abc的文件,应用程序将与文件名作为唯一参数推出,但后来当我重新启动(由pressing我的按钮1 ), GetCommandLineFile()将返回一个空数组。

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.

推荐答案

我相信 Application.Restart 是专为替代ClickOnce应用程序如何处理它标准的命令行参数。

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

纵观微软的$ C $下 Application.Restart ,他们明确检查,如果应用程序是一个ClickOnce应用程序,然后重新启动它的任何参数传递。任何其他应用程序,获得 Environment.GetCommandLineArgs()解析,并发送至一个新的进程。

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

这样的话,当你的应用程序启动时, GetCommandLineFile()应该再抢的参数。

这篇关于Application.Restart没有传递参数回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆