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.
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.