ClickOnce application will not accept command line arguments

I have a VB.NET application that accepts command line arguments.

It works great when debugging when disabling Visual Studio ClickOnce security settings.

The problem occurs when I try to install the application on the computer using ClickOnce and try to start it using the arguments. I get a crash when this happens (ooh!).

There is a workaround for this problem: move the files from the latest publishing folder to computer C: drive and remove the ".deploy" from the .exe. Run the application from the C: drive and it will handle the arguments just fine.

Is there a better way to get this to work than the workaround I have above?

Thanks!

+3
source share
1 answer

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 } 
+3
source

All Articles