So the need for a client's offline ClickOnce app to have command line parameters came up again. (oh boy).
The Microsoft docs initially spec'd out that you can't do this when the ClickOnce app is an "offline" app. But since .NET 3.1, there is a trick you can use to make offline ClickOnce command line parameters function.
The scenario is this:
- Client has a ClickOnce app that gets updated (redeployed) by their developer frequently
- 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".
- 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.
The solution, initially, I thought was to simply have the developer put some time based conditions actually in the application code itself. 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.
Finally I discovered and solved it like this:
Alter the app's Main to look like this:
static void Main(string[] argsx)
{
//Handle CMD Line Params
try
{
var argList = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0];
argList = argList.ToLower();
var args = argList.Split(',').ToList();
if (args.Contains("early")) RunOnAuto_Early(); //includes an Environment.Exit(100); to prevent the set of main from running;
}
catch (Exception ex)
{
//do something meaningful here
}
//regularly scheduled program features
OpenMenu();
}
Now the trick to actually pass the arguments is a little funny.
1.) Find the app on the production machine's Start Menu
2.) Explore its containing folder (it should be something in C:\users\[user]\AppData\.....\Start Menu\Programs\[your app]), note this path
3.) Note the filename of the shortcut, its file extension should be .appref-ms
4.) You now have what you need.
Here's how to make it work now using a batch file example
1.) Create a batch file
2.) Paste in the path from step #2 above
3.) Paste in the shortcut filename (including it's file extension)
4.) It should look something like this:
C:\Users\[username]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\[your app]\[your app].appref-ms
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
6.) Add a space, and your parameters. NOTE: the parameter CANNOT contain spaces or double quotes ("). If you need to use multiple params, use a CSV list (e.g. param1,param2, param3). Your batch file should now look something like this:
"C:\Users\[username]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\[your app]\[your app].appref-ms" early,bob,test
7.) Now the function in Main() will parse early,bob,test and you can do whatever you'd like with them.
CHEERS and Happy Programming!