Adam Wood
Adam Wood

Reputation: 289

How can I read the command line supplied to an offline clickonce application?

I have a simple application that opens a TCP connection and communicates via Telnet to another system. The application is to read a file that contains parameters and a simple scripting language to issue commands based on prompts from the connection.

Written in VS2013 using .NET 4

My application works as designed with one little exception.

I am publishing to a location using VS2013 which works well enough but the idea is to read a command line passed to my application that contains the path/file for the script to execute and that doesn't work as expected.

Finding out the hard way, the standard args[] parameters are not passed when it's published this way.

I have searched out multiple solutions that don't work both on here and other sites.

This is the basis (excerpt from page) of my current implementation to read the command line (found here: http://developingfor.net/2010/06/23/processing-command-line-arguments-in-an-offline-clickonce-application/). This seems to be similar to all solutions I've found, each with some variation that doesn't work.

string[] args = null;
if (ApplicationDeployment.IsNetworkDeployed)
{
    var inputArgs = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData;
    if (inputArgs != null && inputArgs.Length > 0)
    {
        args = inputArgs[0].Split(new char[] { ',' });
    }
}
else
{
    args = e.Args;
}

This SHOULD return args[] with parameters passed. I believe it would also include the actual command with path to the application. The Split function is because the author wishes to pass arguments separated by commas and not spaces.

My incarnation of this is a bit longer to include some checks to see if we actually get arguments from being compiled as an exe instead. If I compile to EXE and supply a command line, all is fine. Here is my code, not very concise as I've made lots of changes to debug and make this work.

I haven't figured out how to debug in the ide as network deployed with a command line so my debug code is via messagebox.

[STAThread]
static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    if (args.Length == 0)  //If we don't have args, assume onclick launch
    {
        if (ApplicationDeployment.IsNetworkDeployed)  //are we oneclick launched?
        {
            var cmdline = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData;  //this should hold the command line and arguments????
            if (cmdline != null && cmdline.Length > 0) //we have something and it contains at least 1 value
            {
                //This is all debug code to see what we get since we can't trace in this mode
                MessageBox.Show(cmdline.Length.ToString());  //how many objects do we have?
                foreach (String s in cmdline)
                {
                   MessageBox.Show(s); //show us the value of each object
                }
                Application.Run(new frmMain(args)); //launch the form with our arguments
            }
            else
            {
                //quit application
                MessageBox.Show("No command line.1");  //debug so we know where we failed
                Application.Exit();
            }
        }
        else
        {
            //quit application
            MessageBox.Show("No command line.2"); //debug so we know where we failed
            Application.Exit();
        }
    }
    else
    {
        Application.Run(new frmMain(args)); //launch form with args passed with exe command line
    }
}

Running the code above like this:

sTelent.application 1234

I have also explored the URL passing method which seems to only apply if launched from a web server, which this application is not.

At first I got NULL for my object: AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData

After more research I discovered that in my project properties under the Publish section there is an option button and under Manifests I can choose "Allow URL Parameters to be passed to application"

I checked this box and while I get different behavior, I don't get the desired behavior.

With that option checked I now get 2 messages boxes: The first showing the number of objects in cmdline and that number is 1 and the second showing the value of that one object which contains only the path/command to my application. No other objects and definitely not my arguments.

Am I totally off base? How do I get my command line arguments from an offline clickonce published application?

Upvotes: 2

Views: 1234

Answers (1)

glenderson
glenderson

Reputation: 11

It seems that you must put the argument on the .appref-ms and not on not the .application or .exe for this to work correctly for clickonce based applications.

I created a short cut on my desktop by copying the installed application link found under All Programs. That should create an icon on your desktop with the same name as your application.

Then, open a command prompt, type in “%userprofile%\Desktop\My App Name.appref-ms” word for word (of course replace "my app name" with your application name). It should then pass the arguments. You can also put the command within a .bat file. I'm sure that you can also reference the link directly, it typically is located under c:\users[user profile]\appdata\roaming\Microsoft\windows\start menu\programs[app name]

Upvotes: 1

Related Questions