Application.Restart 不传回参数
这是一个 ClickOnce 应用程序。根据文档,“如果您的应用程序最初在首次执行时提供了命令行选项,则重新启动将使用相同的选项再次启动该应用程序。”。但我不知道这是否适用于 ClickOnce 应用程序。如果是这样,我做错了什么?
这是我的代码:
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
文件时,应用程序将启动,并以文件名作为唯一参数,但是当我重新启动时(通过按 button1
),GetCommandLineFile()
将返回一个空数组。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信
Application.Restart
是为标准命令行参数而设计的,而不是 ClickOnce 应用程序如何处理它。查看 Microsoft 的
Application.Restart
代码,他们明确检查应用程序是否是 ClickOnce 应用程序,然后重新启动它,不传递任何参数。任何其他应用程序都会解析Environment.GetCommandLineArgs() 并将其发送到新进程。我认为更好的解决方案不是将参数写入文件,而是简单地启动一个新进程:
这样,当您的应用程序启动时,
GetCommandLineFile()
应该再次获取参数。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, getsEnvironment.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 :
That way, when your application starts up,
GetCommandLineFile()
should grab the arguments again.