When .Net 2.0 launched several years ago, WinForm developers got a brand new technology baked into the framework that allowed WinForm applications to be deployed similar to a Web application. The technology was called ClickOnce. ClickOnce has seen a few enhancements since it’s initial release such as the ability to add a desktop icon when an application is installed along with better support for ISVs. One feature still missing from ClickOnce is to make an application start automatically after login. Let’s solve this problem.
When a ClickOnce application is installed the user doesn’t know the directory location it is installed in on their computer. Honestly they shouldn’t care. Not only does the user not know, but the developer doesn’t know either! ClickOnce applications are installed in random directories on each computer in a hidden folder within the user account called the “AppData” folder. For example, here is a screen shot where the ClickOnce version of Witty Twitter that I publish on this site is installed on my newly built machine.
As you can see it is in a random place and this is the problem because a ClickOnce application doesn’t have a fixed path like c:\program files\somewhere\myapp.exe. Why? Well, because the ClickOnce team at Microsoft didn’t build in the ability for Developers to specify a location as to where the application is installed, nor can the user select one. The purpose of a ClickOnce app is the end-user clicks a URL that ends in .application and the application installs. Simple, quick, easy. If the application path was set outside of the user’s account it would require escalated permissions. A feature of ClickOnce is it allows the user to install an application without administrative permissions on the computer (great for internal apps at companies for example). Since the ClickOnce apps are installed in the user’s local directory and randomized as to their whereabouts, we can’t place a shortcut URL in the startup folder that points to the executable.
Now some of you might be thinking that a Developer can programmatically figure out the location of the executable and place the shortcut in the Startup folder within the menu, thereby allowing the application to start. That would be the wrong thing to do. The reason is ClickOnce applications version themselves as they are installed. If you took this approach, the next time the application was updated, a new version would be installed and the old version still remains. Essentially there are two versions on the machine (this is a feature because it allows end-users to roll back to the previous version if they want to in case the latest version of the application is broke). As you see, creating a shortcut programmatically and placing it in the startup folder on the machine won’t work because it is constantly changing locations.
The solution to the problem is actually really simple. So simple in fact it escapes most.
Remember that a ClickOnce application is launched from a URL ending in .application. For example, the Witty Twitter application I publish has a URL like the following:
The .application extension is something your computer knows how to process after the .Net framework gets installed.
What most developers do is they open up the folder the location where the ClickOnce application installed the shortcut to view the shortcut in notepad. Here’s an example using Witty:
If we view the Witty shortcut in Notepad++ it looks like this:
The first thing you’ll notice is the extension of this shortcut is different, it ends with .appref-ms. So what happens is developers copy this file to the startup folder, but then it doesn’t work in all cases. It will work on some versions of Windows but not others.
Here’s the fix. Right click on your desktop and in the menu create a new shortcut.
Next, enter the URL of your application. Here’s a sample using Witty:
Click next and give it a name:
After saved, open the shortcut in notepad and remove everything but the first two lines. You should have something like this left when you get done:
Now you have your shortcut. Wait, that’s just a standard Internet Shortcut? Right! That’s all you need since the application is launched from a URL anyway. You’ll notice after doing this the new shortcut is showing the default browser’s icon (not your application icon), this is perfectly ok. Now that you have your file, simply drag and drop the shortcut into the Startup folder. The next time the computer boots, the application will automatically launch.
If you are a developer you are home free now because programmatically writing a file to the user’s Startup folder with this information is simple. In your application you can place a menu option to start the application on startup or not.
Their are two downsides using the method I can think of. One, your application shows the default browser’s icon. Secondly, when the computer launches the browser will open for a split second to launch the application but then it closes (at least that’s been our experience).