Integration Experts – Dipesh Avlani

BizTalk Server, WCF, WF and Cloud computing…

Clickonce and startup on windows logon (Vista)

Posted by DipeshA on May 12, 2009


Having issues getting your clickonce applications auto start when logging on to Vista? You have come to the right place! Below is an explanation of various approaches out there and the pros ans cons of each approach and lastly the best working approach!

Solution 1: Placing a shortcut in the startup folder.
One way to fix this problem was to programmatically create a shortcut in the users startup folder.
string publisherName = Application.CompanyName;
string productName = Application.ProductName;
string startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
startupPath = Path.Combine(startupPath, productName) + “.appref-ms”;
if (!File.Exists(startupPath))

{
string allProgramsPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
string shortcutPath = Path.Combine(allProgramsPath, publisherName);
shortcutPath = Path.Combine(shortcutPath, productName) + “.appref-ms”;
File.Copy(shortcutPath, startupPath
);

}
Whist this was a good approach but this still has issues.
Issue 1:
Dint work on all windows platforms. Some of my tests failed on one box with Vista Enterprise and strangely worked on another box with Vista Enterprise!

Issue 2:
Uninstalling the application dint remove the shortcut from the startup folder. Thus this would always pop up the installation of the app everytime you login even though you had uninstalled the application.

Solution 2: Creating a url shortcut and placing in the startup folder.
On digging deep in google, I found an approach where you could create “url” shortcuts and place them in the startup folder. Whist this worked reliably on most windows platform, there was a catch!

Issue 1: This approach only works if you are connected to the internet at the time of logging in to windows.

Issue 2:
Uninstalling the application dint remove the shortcut from the startup folder. Thus this would always pop up the installation of the app everytime you login even though you had uninstalled the application.

Solution 3: Using the registry and turning off check for updates (Best approach).
Ok this is the best solution till now as per my experience. This works on all versions of vista where the above two approaches failed.

Just like solution1, you need to create a appref-ms shortcut. But this time it has to be in the current users registry as opposed to the startup folder. Also this requires you to turn off check for updates through clickonce for it to work fully.

Creating the shortcut:
RegistryKey regkey = Registry.CurrentUser.OpenSubKey(“SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run”, true);
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”;
regkey.DeleteSubKey(“YourApplication”, false);//delete old key if exists
regkey.SetValue(“YourApplication”, shortcutPath);


Once you have this in place, you need to ensure you uncheck the option “Application should check for updates” in the publish properties of your application. This is important. All this means is, you will have to implement code to check for updates when your application starts. This is easy and you can use Application.Deployment namespace to achieve this. Probably fire this “update check” code in your splash screen and force an application.restart if the application was updated. You can make this update process interactive by using Async calls and refreshing you splashscreen with progress bar updates by subscribing to progress events.
Form Load:
ApplicationDeployment.CurrentDeployment.UpdateProgressChanged += new DeploymentProgressChangedEventHandler(UpdateProgress);

Form Shown:
ApplicationDeployment.CurrentDeployment.CheckForUpdateAsync();

private void UpdateProgress(object sender, DeploymentProgressChangedEventArgs e)
{
progressbar1.Value = e.ProgressPercentage;
label1.Text = (e.BytesCompleted / 1024).ToString() + ” of ” + (e.BytesTotal / 1024).ToString() + ” kb downloaded”;
}


After doing this you should be all good to go!

I havent found any issues with this approach till now. Also this approach overcomes the issues with the above two solutions.

About these ads

16 Responses to “Clickonce and startup on windows logon (Vista)”

  1. Lysaar said

    Great work! :) Thanks a lot

  2. Matt said

    Question for option #3: why is it necessary to disable the built-in checking of updates?

  3. Rorik said

    Thanks for this article, very helpful. In response to Matt's question, if you leave the built-in checking of updates, the app won't launch. Took me a while to figure that out.

  4. Dipesh A. said

    If you leave the clickonce updates on, the application does not launch automatically.

  5. Sal said

    Instead of “regkey.DeleteSubKey(…….)”
    use regKey.DeleteValue(…..);

  6. Rich said

    How do you create a .appref-ms file? Can you explain that please.

    I would really like to use your write up.

    Thanks.

    • DipeshA said

      Hi Rich,

      You dont have to create the .appref-ms file. This file is created automatically when you deploy an application through clickonce. The difference between application reference files (.appref) and shortcuts (.lnk) is that the application reference points to the original application URL and not the location of the EXE on the disk. This depends on settings in the ClickOnce manifest.
      The clickonce manifest can be found in the ‘Project Files’ folder in your windows forms project (once setup for clickonce).

      Dipesh

  7. Rich said

    Thank you for the response, could you possible show this in an example project. That would help me understand more to what I am trying to do. Hopefully it isn’t to much work for you. It would be much appreciated though.

    Once again thank you for your reply.
    -Rich

    • DipeshA said

      Hi Rich,

      I am afraid I dont have a sample project handy. However, I would suggest you follow this article up till the point where you get to choose whether you require automatic application updates. Uncheck the option “Check of application updates” and follow steps in my post as is. This should get you started.

      Cheers,

      Dipesh

  8. Rich said

    For anyone else who has issues I figured this out.

    You may need to use all the following namespaces.

    using System.Deployment.Application;
    using System.IO;
    using Microsoft.Win32; //For Registry

    Also
    This code below can go directly in your program. It doesn’t have to be linked to an external file or shortcut (where i was getting confused on.. but I am a complete noob!!) like:

    private void Form1_Load(object sender, EventArgs e)
    {
    RegistryKey regkey = Registry.CurrentUser.OpenSubKey(“SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run”, true);
    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”;
    regkey.DeleteSubKey(“YourApplication”, false);//delete old key if exists
    regkey.SetValue(“YourApplication”, shortcutPath);
    }

    Thank you to Dipesh for your replies, I am such a noob you probably didn’t understand what I was asking because I didn’t know how to explain it.

    • DipeshA said

      Hi Rich,

      Glad it helped. Just on the above, the reason I dint encapsulate the above code in form load was because you dont want to be creating shortcuts everytime your app starts. Something that you might want to keep in mind.

      Regards,

      Dipesh

  9. Jeff Agee said

    That helps. I have a question, if you have time. Originally I was using “Application.ExecutablePath” in the run key value, which pointed to the local “.exe” file instead of the “.appref-ms”. The program did autostart but the saved userscope my.settings didn’t show on autostart. When I use “.appref-ms” as you indicate, the program autostarts with the saved userscope my.settings, which is great. Have you any idea why this is? Either way, thanks for the article.

  10. Ryan said

    great article! I just have a question on where do you specify the url where the app is supposed to check for updates?

  11. Camilla said

    Very nice blog post. I certainly love this site. Stick
    with it!
    click the following article
    and click the following web page
    and Click That Link
    and also click the following web page

  12. Howdy would you mind letting me know which web host you’re using? I’ve loaded your blog
    in 3 different browsers and I must say this blog loads a lot quicker
    then most. Can you recommend a good web hosting provider at a reasonable price?
    Thanks, I appreciate it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: