Techemistry Blog

Offline ClickOnce app with command line parameters

3/9/2015 9:46:00 PM -- Ted Krapf

<p>So the need for a client's offline ClickOnce app to have command line parameters came up again. (oh boy).</p> <p>The Microsoft docs initially spec'd out that you can't do this when the ClickOnce app is an "offline" app. &nbsp;But since .NET 3.1, there is a trick you can use to make offline ClickOnce command line parameters function.</p> <p><strong>The scenario is this:<br /> <br /> </strong></p> <ul> <li>Client has a ClickOnce app that gets updated (redeployed) by their developer frequently</li> <li>The app is Offline, meaning during publishing from Visual Studio, "How will users install the application?" is set to "From a CD-ROM or DVD-ROM", and then "The application will not check for updates".</li> <li>They have Task Scheduler running on their production box and they basically want the app to run at two different times daily, but to have a command line switch to run different features of the app at the different times.</li> </ul> <p ><br /> The solution, initially, I thought was to simply have the developer put some time based conditions actually in the application code itself. &nbsp;But ultimately this wouldn't fit the client's needs because sometimes they want to run/launch the app manually, and that could happen at any random time of the day.<br /> <br /> </p> <p><strong>Finally I discovered and solved it like this:<br /> <br /> </strong></p> <p>Alter the app's Main to look like this:<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ffff00;">static void Main(string[] argsx)<br /> &nbsp; &nbsp; &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Handle CMD Line Params<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;try<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var argList = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0];<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; argList = argList.ToLower();<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var args = argList.Split(',').ToList();<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (args.Contains("early")) RunOnAuto_Early(); &nbsp;//includes an Environment.Exit(100); to prevent the set of main from running;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex)<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //do something meaningful here<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //regularly scheduled program features<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OpenMenu();<br /> &nbsp; &nbsp; &nbsp; &nbsp; }</span></p> <p><br /> </p> <p><strong>Now the trick to actually pass the arguments is a little funny.</strong><br /> 1.) Find the app on the production machine's Start Menu<br /> 2.) Explore its containing folder (it should be something in C:\users\[user]\AppData\.....\Start Menu\Programs\[your app]), note this path<br /> 3.) Note the filename of the shortcut, its file extension should be .appref-ms<br /> 4.) You now have what you need.<br /> <br /> <br /> <strong>Here's how to make it work now using a batch file example</strong><br /> 1.) Create a batch file<br /> 2.) Paste in the path from step #2 above<br /> 3.) Paste in the shortcut filename (including it's file extension)<br /> 4.) It should look something like this:<br /> &nbsp; &nbsp; C:\Users\[username]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\[your app]\[your app].appref-ms<br /> 5.) Be sure to surround the path+filename with double quotes if you're using a batch file - it contains spaces, and the cmd prompt doesn't like spaces<br /> 6.) Add a space, and your parameters. &nbsp;NOTE: the parameter CANNOT contain spaces or double quotes ("). &nbsp;If you need to use multiple params, use a CSV list (e.g. param1,param2, param3). &nbsp;Your batch file should now look something like this:<br /> &nbsp; &nbsp; "C:\Users\[username]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\[your app]\[your app].appref-ms" early,bob,test<br /> 7.) Now the function in Main() will parse early,bob,test and you can do whatever you'd like with them.<br /> <br /> <br /> CHEERS and Happy Programming!<br /> &nbsp; &nbsp;</p>

[return to articles list]