ClickOnce application won't accept command-line arguments

做~自己de王妃 提交于 2019-12-05 16:29:13

"Command-line arguments" only work with a ClickOnce app when it is run from a URL.

For example, this is how you should launch your application in order to attach some run-time arguments:

http://myserver/install/MyApplication.application?argument1=value1&argument2=value2

I have the following C# code that I use to parse ClickOnce activation URL's and command-line arguments alike:

public static string[] GetArguments()
{
    var commandLineArgs = new List<string>();
    string startupUrl = String.Empty;

    if (ApplicationDeployment.IsNetworkDeployed &&
        ApplicationDeployment.CurrentDeployment.ActivationUri != null)
    {
        // Add the EXE name at the front
        commandLineArgs.Add(Environment.GetCommandLineArgs()[0]);

        // Get the query portion of the URI, also decode out any escaped sequences
        startupUrl = ApplicationDeployment.CurrentDeployment.ActivationUri.ToString();
        var query = ApplicationDeployment.CurrentDeployment.ActivationUri.Query;
        if (!string.IsNullOrEmpty(query) && query.StartsWith("?"))
        {
            // Split by the ampersands, a append a "-" for use with splitting functions
            string[] arguments = query.Substring(1).Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries).Select(a => String.Format("-{0}", HttpUtility.UrlDecode(a))).ToArray();

            // Now add the parsed argument components
            commandLineArgs.AddRange(arguments);
        }
    }
    else
    {
        commandLineArgs = Environment.GetCommandLineArgs().ToList();
    }

    // Also tack on any activation args at the back
    var activationArgs = AppDomain.CurrentDomain.SetupInformation.ActivationArguments;
    if (activationArgs != null && activationArgs.ActivationData.EmptyIfNull().Any())
    {
        commandLineArgs.AddRange(activationArgs.ActivationData.Where(d => d != startupUrl).Select((s, i) => String.Format("-in{1}:\"{0}\"", s, i == 0 ? String.Empty : i.ToString())));
    }

    return commandLineArgs.ToArray();
}

Such that my main function looks like:

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        var commandLine = GetArguments();
        var args = commandLine.ParseArgs();

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