Command Line Arguments only work with the ClickOnce application when it starts from a URL.
For example, this is how you should run the application to attach some arguments at runtime:
http: //myserver/install/MyApplication.application? argument1 = value1 & argument2 = value2
I have the following C # code that I use to parse ClickOnce activation URLs and command line arguments:
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(); }
So my main function looks like this:
/// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { var commandLine = GetArguments(); var args = commandLine.ParseArgs(); // Run app }
Reddog
source share