Simulating command line parameters in Click Once applications

The impossibility of send command line arguments to an application in a standard way is one disappointed thing about ClickOnce. However we can send query string arguments in order to simulate command line arguments.

First, we will need to enable the application to receive parameters through URL. In order to do this we have select Publish page in the properties of the project. Then we will select options and we will check Allow URL parameters to be passed to application.

image

Now we can get the query string using the ApplicationDeployment class and parse it simulating command line arguments, for example we can get the following static method in a CommandArguments class:

public static string[] GetArguments()

{

    if (ApplicationDeployment.IsNetworkDeployed &&

        ApplicationDeployment.CurrentDeployment.ActivationUri != null)

    {

        string query = HttpUtility.UrlDecode(

            ApplicationDeployment.CurrentDeployment.ActivationUri.Query);

        if (!string.IsNullOrEmpty(query) && query.StartsWith("?"))

        {

            string[] arguments = query.Substring(1).Split(' ');

            string[] commandLineArgs = new string[arguments.Length + 1];

            commandLineArgs[0] = Environment.GetCommandLineArgs()[0];

            arguments.CopyTo(commandLineArgs, 1);

            return commandLineArgs;

        }

    }

    return Environment.GetCommandLineArgs();

}

The main idea is detect if the application is installed through ClickOnce and if it is get the arguments from the query string, in any other case the parameters are retrieved through the standard way.

Then, if we send the following URL http://www.misite.com/MyApplication.application?-config, it would be the same that execute the application using arguments:

MyApplication.exe -config

The CommandArguments class allow us abstract how the arguments are sent and if the application is installed through ClickOnce or not.

6 Comments

  • But how do I send such a command from a desktop C# application?

  • question, lets say that, if I want to call the click once application from a web site and pass parameters, in that case how will it work? I can understand that if the application is been installed for the first time or upgrate the parameters get passed down. But if the application is already installed and I am trying to pass a parameter using the same pulish location the program doesn't seem to accept the parameters. How can we go around to get the parameters passed to the click once program if it already installed or not.

  • It's really the same, the ClickOnce intalls always check the site, the you can run the aplication using the URL, either there is not updates. e.g.:

    http://www.misite.com/MyApplication.application -secondary

  • I am trying to do this with a WPF app that I made. But I don't seem to have access to the HttpUtility class....how did you do that? Was that code just for silverlight or something?

  • You need to add a reference to the System.Web assembly.

  • Yes, this was very helpful. Thank you!

Comments have been disabled for this content.