【问题标题】:AppDomain.CurrentDomain.SetupInformation.ActivationArguments returns null for one ClickOnce application but not the otherAppDomain.CurrentDomain.SetupInformation.ActivationArguments 为一个 ClickOnce 应用程序返回 null 而不是另一个
【发布时间】:2015-01-21 17:46:52
【问题描述】:

我有两个 WinForm ClickOnce 应用程序,最初使用 Visual Studio Express 2008 (C#) 开发,现在使用 VS Express 2013 进行维护。两者都针对 .NET Framework 4 客户端配置文件。

请考虑下面的代码,我尝试通过在 InitializeComponent() 之后插入一行代码并在 VS 中单击“开始”来运行和调试应用程序来获取 ActivationArguments:

public Form1()
{ 
    InitializeComponent();                
    ActivationArguments actArgs = AppDomain.CurrentDomain.SetupInformation.ActivationArguments;
    etc...
}

我很困惑为什么一个应用程序为 ActivationArguments 返回 null 而另一个应用程序返回预期的对象。两者都是简单的 WinForm 应用程序,简单的 C#,相同的部署设置,没什么花哨的。目标是最终获得 ActivationData,但我首先需要一个 ActivationArguments 对象才能到达那里。

有什么想法吗?

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    以下内容适用于 C# WinForms App .Net 4.5,该应用程序使用 ClickOnce Publishing 用于在项目的“发布设置”中设置为“..available offline as well..”的应用程序。

    1) 在项目设置中指定或从控制台传递的命令行参数可以通过在 main() 中添加 args 在 program.cs 中读取

    static void Main(string[] args) // in program.cs, args[0] is first argument
    

    或通过使用

    string[] args = Environment.GetCommandLineArgs(); // in Form1.cs e.g. in Form1_Load, args[1] is first argument
    

    2) 命令行参数通过例如双击资源管理器/桌面中的关联文件可以这样读取:

    using System.Deployment.Application;
    
    // e.g. in Form1_Load
    if (ApplicationDeployment.IsNetworkDeployed)
    {
        string[] activationData = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData;
        if (activationData != null && activationData.Length > 0)
        {
            string[] args = activationData[0].Split(new char[] { ',' });
            if (args.Length > 0)
            {
                // args[0] is first argument
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-03
      • 2020-02-23
      • 2016-11-16
      • 2020-07-04
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 2018-12-05
      相关资源
      最近更新 更多