Creating a desktop shortcut for a Click Once application

This GoldMail shows how to create a desktop shortcut for your ClickOnce application. The code can be used for any ClickOnce application, assuming you set your attributes accordingly.
 
 
This download contains the sample code (VS2008, C#). If you are a VB developer and can’t figure out how to translate this to VB, post a query and I’ll see what I can do for you.
ClickOnce_DesktopShortcut.zip

July 19, 2009 edit: Here is a link to the follow-up post that provides the code and implementation details for doing same in VB.

ClickOnce Desktop Shortcut Using VB

[edit 7/7/2011 Move zip file to Azure blob storage]

[edit 3/8/2014 Move to different Azure blob storage]

Tags:

49 Responses to “Creating a desktop shortcut for a Click Once application”

  1. Robert Cezar Says:

    Hi,

    I am a VB.Net programmer and have never even examined C#. I’ve been trying to find a resolution to this shortcut issue for some time.

    I would REALLY appreciate any assistance you could afford in implementing this in VB.Net 2008. My application is distributed using the Dot.Net 2 Framework.

    I thank you very much in advance for any help you can offer.

    Sincerely,

    Robert Cezar

  2. Robert Cezar Says:

    Thank you very much. I really appreciate it.

    Robert

    • robindotnet Says:

      I have posted a follow-up with the code for the desktop shortcut in VB, and how to implement it. There is a link to the followup on the original article. Please let me know if you have any questions.

  3. ClickOnce Desktop Shortcut using VB « RobinDotNet’s Blog Says:

    […] Desktop Shortcut using VB By robindotnet This is a follow-up post to the article How to Create a Desktop Shortcut for a ClickOnce Application, providing the VB version of the code, as requested by one of the readers of this […]

  4. Robert Cezar Says:

    Hello Robin,

    Well, it worked PERFECTLY! Thank you so very much for your help… I was pulling my hair out (not much left) with this problem.

    As a small note of appreciation, we’ve posted our thanks in the Kudos section of our Web site.

    http://www.ItsYourPlane.com/kudos.asp

    PS. If you’re into flight simulation, you’re welcome to a FREE registration!

    Thanks again.

    Robert

  5. robindotnet Says:

    Hello Robert,

    I’m glad it was helpful to you. I hope the implementation info was, too. It’s been awhile since I did VB (we use mostly C# at GoldMail, except the Office Add-ins, which are in VB), so I had to think about how to implement it; I didn’t want to assume that people would know. It’s actually a joy to do VB again, and it’s nice to hear of a development shop using it. One of my friends at MSFT tells me there are more VB devs than C#. 🙂 And thanks for the kudos on your website; I appreciate it!

    Also, thanks for the free offer, but unfortunately I already know that I am terrible at flying. Remember the Microsoft Flight Simulator that came out in the 80’s? I used to try to play that, and I always crashed. But I’ll keep your offer in mind in case I want to try again! I think they recently discontinued theirs, so it’s nice to know there’s another version available on the market.

    If you have any further problems with ClickOnce, please feel free to post your question to the MSDN ClickOnce and Setup & Deployment forum, and I’ll help you out. You can ping me here if you do that, to make sure I see it, although I try to respond to most of the postings.

    Robin

  6. Paul Says:

    Thank you very much, Ive been trying to figure out how to do this with vb.net for the past week. Very simple and easy to follow.

  7. Steve Says:

    Thanks! This was very helpful and easy to follow.

  8. Hitchhiker Says:

    Maybe I am just missing the point.?. We have all asked how to create a desktop icon when you publish an application. Although this is nice code and does work, it does not address the issue. If I have a user that needs to run the application from the desktop and it goes missing, they call me for support. Once we run the application from whereever, it creates the desktop icon but they still call me next week when it dissappears again. So is there a way (in .NET 2.0) to create this as you install or auto run another application that creates the shortcut for you?

    • robindotnet Says:

      Why is your user’s desktop shortcut disappearing? Are you using the code I’ve posted here? It creates the shortcut whenever the user gets an update or when he installs the application for the first time. That is what IsFirstRun tells you.

      I wrote it like that because it seems to me like the shortcut disappears and must be recreated whenever the user gets an update. I suspect that when Microsoft added the autocreation of the shortcut in .Net 3.5, in order to save themselves the trouble of figuring out if something about it changed, they just remove it when the application starts to install, and then if you have set that property in the deployment, it recreates it. We aren’t using the auto-creation because we are still targeting the .NET 2.0 Framework, so my code recreates the shortcut.

  9. Chris Says:

    Seems like this wouldn’t work in Visual C# Express Edition because I can’t find those options.

  10. cavo789 Says:

    Great explanations ! Thanks for the sharing.

  11. J Rose Says:

    Sorry if I’m posting this twice . . but the other place I posted it doesn’t have any other comments…
    I am trying to use the VB version of the code and when installing the click once deployment on our machines, we are getting the following error:
    *** System.IO.FileNotFoundException: Could not find file ‘C:\Documents and Settings\jrose\Start Menu\Programs\Generation Mortgage Company\Generation Equity Mortgage System.appref-ms’.
    File name: ‘C:\Documents and Settings\jrose\Start Menu\Programs\Generation Mortgage Company\Generation Equity Mortgage System.appref-ms’
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite)
    at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
    at Gfm.Gems.UI.My.MyApplication.CheckForShortcut()
    at Gfm.Gems.UI.My.MyApplication.MyApplication_Startup(Object sender, StartupEventArgs e)
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnStartup(StartupEventArgs eventArgs)
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()

    ANY help would be greatly appreciated!

    • robindotnet Says:

      This might benefit someone NOT using the VB version, so I’m going to answer this question here as well.

      The code basically looks for the shortcut on the start menu and copies it to the desktop. To locate the entry on the start menu, it has to have the name of the folder and the name of the shortcut itself.

      In a ClickOnce deployment, the Publisher Name is used for the name of the folder, and the Product Name is used for the name of the actual shortcut.

      Rather than hardcoding those values, the code for the shortcut uses Reflection to retrieve the corresponding information from the Assembly. It assumes the Assembly Company matches the Publisher Name, and the Assembly Description matches the Product Name.

      From the error you are showing here, that’s not the case. It looks like it can’t find the shortcut it wants to copy. So check your Assembly information against your Publish Options and make those two values match, and it should work.

      • J Rose Says:

        Thank you so much for your help! It is greatly appreciated. That worked. You were correct . . the Description didn’t quite match my product name.

        • robindotnet Says:

          Thanks; I’m glad I was able to help you!

          • Zabian Says:

            Hi Robin!

            It´s necessary the code line “ConfigManager.LoadConfigSettings();”, right?

            Cause my ClickOnce app don´t let me use it. Says ConfigManager doesn´t exists in the context.

            I use System.Deployment.Application and System.Reflection.
            Please, help me.

          • robindotnet Says:

            No, that must be a remnant from the code I copied it from. Is that in the code I posted?? I’ll remove it. Thanks for letting me know!

  12. Sasi Says:

    HI,
    Thanq for ur help,But one more thing how to remove desktop icon when we uninstall that appliction.

  13. Reza Says:

    Hi Robin,

    Thanks for your helpful article. I have another Problem; I have an application that many users are working with that. I recently changed it so it can publish and install with click once. Your Article resolved one of my big troubles (About putting shortcut on the desktop). But I want to auto start and perform Installing click once progress. (Ideal scenario is a silent installation that user never sense anything).
    Can you help me about it?

    Regards

    • robindotnet Says:

      There is a way to have a ClickOnce app start when the computer starts up. However, you might have problems getting your updates if the internet connection doesn’t come up faster than the ClickOnce app. What type of application is it and what does it do that you need it to run at startup?

  14. Nikhil Says:

    Hey Robin,

    Thanks for the article great stuff! But I am having one issue. The icon is created on the first run but whenever I update the app using clickonce the desktop icon is removed and it is not created again since it is not it’s first run. Is there a work around this?

    • robindotnet Says:

      Hi. When Microsoft added the auto-desktop-shortcut-create function in .NET 3.5, they also changed the ClickOnce engine to remove the desktop shortcut every time there was an update, probably so they could just create a new one without worrying about it already being there. So just take out the check for IsFirstRun and let the code create the desktop shortcut every time the app runs.

      • Nikhil Says:

        I am running visual studio 2005, 2.0 framework. Is there anyway I can do this ?

        • robindotnet Says:

          I wrote the code with VS2005 and .NET 2.0, so it should work with those. (As noted above, MSFT changed it to remove the shortcut with .NET 3.5, so you just need to take out the check for IsFirstRun and let it create the shortcut each time it runs).

          • Nikhil Says:

            Yeah I the icon is created at first run but when I update the application, it is removed and then not created again..Anyways, thanks for your time.

  15. Farhan Ahmed Says:

    I am using Visual studio 2008 in Windows 7. I dont get an option to check create desktop shortcut and even i dont have that manifest tab itself. And when i run the same Visual Studio in Windows xp i get the option. Please let me know is there an option to include as i am using framwork 3.5 only.

  16. Alex Says:

    Thanks Robin! I’m targeting NET 2.0 so this comes really helpful.

    • robindotnet Says:

      You’re welcome. Even though we target .NET 3.5 now, I still use this code. Another interesting option — this code basically puts the shortcut back on the desktop every time they run the application. By programming this rather than using the Visual Studio option in later versions of .NET, you can give the user an option to say “I don’t want a desktop shortcut”, and NOT create it when he runs it.

  17. k!Ng Says:

    Hello sir,

    I am getting error bellow with your code:


    The name ‘ConfigManager’ does not exist in the current context

    [Code]
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(“fr”);
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    ConfigManager.LoadConfigSettings();
    CheckForShortcut();
    Application.Run(new ANAHRDB.Forms.Login());
    [/code]

    • robindotnet Says:

      That was a remnant of code from my own project. I rolled my own configuration manager because i don’t like the way ClickOnce handles it. Just take it out.
      Robin

  18. Kyle Says:

    Robin –

    Very nice article. You addressed sp1 for 2008 in a reply above to someone named chris. I am having the same problem he mentioned (2005 version of the options window). I installed sp1 (from your link) and am still dealing with the same window.

    Do you have a suggestion? I am on Windows 7-64.

    thanks ahead of time!

    – kyle

    • robindotnet Says:

      Hi, as noted in my response to Chris, you have to have VS2008 SP-1 to access those fatures. They are not available in VS2005. Can you upgrade all the way up to VS2010 and use the Express version? What version of .NET does your application target?
      Robin

  19. Anonymous Says:

    […] […]

  20. lukas Says:

    This code is not working. I think AssemblyProductAttribute should be used instead of AssemblyDescriptionAttribute.

    • robindotnet Says:

      The code is right and matches the description. Basically, whatever field you use in the Assembly information is the field you have to look for. I’m putting the match in the Assembly Description, so I’m looking at the AssemblyDescriptionAttribute. If you put the name in the Assembly Product information, you would need to look at the Assembly Product Attribute.
      -Robin

  21. harsh.baid Says:

    Reblogged this on Harsh Baid and commented:
    Nice video on Creating a desktop shortcut for a Click Once application

  22. Anthony Says:

    Hi robindotnet. I got a different situation. I have a vb.net application which I used the auto-desktop-shortcut-create function in the clickonce, my porblem is how can I rename the shortcut through code or prgrammatically. I have an application that i change the shortcut name based on user’s permission or type of user. do you have any idea how i can rename the shortcut and keep the name or rename it very update?

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


%d bloggers like this: