Go to content Go to navigation Go to search

Brokenwire.NET::Programming

Run ClickOnce app on startup
· 2009-08-26 14:07 by Thijs Kroesbergen for Brokenwire.NET

While toying with a small ClickOnce application I found out that there wasn’t an easy way to launch the app automatically on Windows startup. Of course I wasn’t the first one to have this problem so I started reading Ben Griswold aka Johnny Coder’s blog post on this topic. He sums up the pros and cons of several solutions nicely. (So go read that now, I’ll wait here.)

After trying out some of his suggestions I came up with the following plan:

I’ll put a shortcut in the Startup folder of the start menu. This shortcut will point to the executable file of my application, but it will start it with an additional parameter (“-L”). When the application detects that it is being run with this parameter it doesn’t show any of its forms, but just starts the .appref-ms shortcut that is already in the start menu. This shortcut will then start the application just as if the user him/herself clicked it.

The good thing about this solution is that this also fools Vista in starting a ClickOnce app on startup (so it WORKS!).

Why is this the best solution?

One small drawback: if the application is uninstalled then the shortcut in the startup folder will be left behind…

Code, show me code! Okay, so here is the code:

To install the shortcut in the startup folder of the start menu I’ve written this Install method. The ShellLink class is a wrapper around the windows API to make a shortcut. The corresponding article about creating and modifying shortcuts using C# can be found on vbaccelerator.com.

public static void Install()
{
  ShellLink shortcut = new ShellLink();
  shortcut.Target = Application.ExecutablePath;
  shortcut.Arguments = "-l";
  shortcut.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath);
  shortcut.Description = "Start TestApp";
  shortcut.DisplayMode = ShellLink.LinkDisplayMode.edmMinimized;
  shortcut.Save(shortcutFileName);
}

Then to have the application launch itself, I've modified the Main method in Program.cs:

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
  if (args.Contains("-L"))
  {
    AppLauncher.Launch();
  }
  else
  {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new TestForm());
  }
}

The “Launch” method that finds the shortcut for the ClickOnce application and starts it via Process.Start. The determination of the location of the .appref-ms file can be a bit tricky, this code works for me but your mileage may vary.

public static void Launch()
{
  string shortcutFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "Start TestApp.lnk");
  string publisherName = Application.CompanyName;
  string productName = Application.ProductName;
  string allProgramsPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
  string shortcutPath = Path.Combine(allProgramsPath, publisherName);
  shortcutPath = Path.Combine(shortcutPath, productName) + ".appref-ms";
  System.Diagnostics.Process.Start(shortcutPath);
}

You can try it out for yourself with this small sample:

ClickOnceStartupTestApp.zip which is also published here: http://www.brokenwire.net/~thijs/clickoncestartup/publish.htm

Have fun!

Permalink -

  1. Nice posts there. Thank’s for the interesting information.


    Hunter    2009-10-12 23:06    #
  2. “One small drawback: if the application is uninstalled then the shortcut in the startup folder will be left behind…”

    thats a big problem not a small problem.


    sem    2009-10-24 20:11    #
  3. Hi Sem, thanks for commenting.
    The size of the problem depends on the situation. I had to choose between no automatic startup at all or leaving the shortcut behind on uninstall, for me this wasn’t a real issue. But I’m all ears for a solution, so if anyone knows how to tackle this one then I’ll be happy to add that to the solution.


    thijs    2009-10-25 14:27    #
  4. That is cool !!!!!


    Przemys?aw Krygier    2010-01-09 17:31    #
  5. It’s a shame there isn’t an actual ShellLink class at that link; apparently it’s a guide on how to DIY one :) Would much rather prefer a downloadable finished class :/


    — rs387    2010-08-17 19:26    #
  6. Thanks for posting this, very helpful

    funny we have to work around this… clickonce is due for an update, I think, and this, among other things, should be included.

    heres the app I did
    http://antix.co.uk/projects/meanie


    Mr Anthony Johnston    2010-11-02 22:48    #
  7. This solution does still not autostart a ClickOnce deployed application with “AutoUpdate” enabled – sorry


    richard    2011-05-27 15:41    #