ClickOnce applications can have arguments passed to it via URL paramters (similar to how we pass paramters to web pages). An example URL looks like:
http://mycompany.com/myapp.application?username=user&pass=pwd
If you are thinking of using this feature, there are a couple of things to keep in mind.
1) URL Paramters are passed to your application only if your application is launched from a URL. This means that if you have an installed application and the user launches the application from the Start Menu shortcut, your application will not get URL paramters.
2) This feature works well for online applications because online applications are always launched using the URL (i.e., online applications are not installed applications and thus do not get Start Menu shortcuts).
3) URL Parameter passing is not enabled by default. In Visual Studio 2005, you can enable it by checking the "Allow URL parameters to be passed to this application" checkbox. You can get to this by clicking on the "Options..." button from the Publish tab. If you are using the MAGE tool, you can do this when you create a deployment manifest by editing the "Deployment Options" UI. If you are using MSBuild to generate your ClickOnce application, you need to set the TrustUrlParametersproperty on the GenerateDeploymentManifest task.
4) Here is an example of a class that lets you get to the URL parameters collection of your ClickOnce application. Note that the application has to be deployed via ClickOnce for the class to return the URL parameters.
using System;using System.Collections.Generic;using System.Windows.Forms;
public sealed class DeploymentHelper{ private static Dictionary<string, string> urlParms;
static DeploymentHelper() { try { Uri appUri = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.ActivationUri; if (appUri != null) { URLParameters = GetQueryStringParametersCollection(); } } catch (Exception) { } }
public static Dictionary<string, string> URLParameters { get { return urlParms; } private set { urlParms = value; } } private static Dictionary<string, string> GetQueryStringParametersCollection() { Dictionary<string, string> nameValueTable = new Dictionary<string, string>();
if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) { string url = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.ActivationUri.AbsoluteUri; string queryString = (new Uri(url)).Query; string[] nameValuePairs = queryString.Split('&'); bool firstVar = true; foreach (string pair in nameValuePairs) { string[] vars = pair.Split('='); if (firstVar) { firstVar = false; if (vars[0].Contains("?")) { vars = new string[] { vars[0].Trim('?'), vars[1] }; } } if (!nameValueTable.ContainsKey(vars[0])) { nameValueTable.Add(vars[0], vars[1]); } } }
return (nameValueTable); }}
You can get to the URL parameters by using the static property URLParameters:
Dictionary<string,string> urlParms = DeploymentHelper.URLParameters;
Note that the core of the class is the static GetQueryStringParametersCollection() method. I copied most of this method from MSDN, however, the MSDN version has a bug in it so and I had to modify it.