Julie Lerman's DevLife

DevLife Part I [May 2005 - March 2007]

My Links

Blog Stats

News

A blog for DevSource.com.

This blog was originally part of the blogs.ziffdavis.com site from May 2005 through June 2007 when the blog was moved to the Movable Type blog engine and hosted at blog.devsource.com/devlife.
The original blog was eventually shut down and I was given the posts so that I could host them on my own site.


Archives

ClickOnce and Desktop Shortcuts - A cheesy way to make it work

[NOTE AN IMPORTANT UPDATE TO THIS METHOD. SEE THIS POST FROM JULY 28]

ClickOnce does not have the ability to create desktop shortcuts. I'm confident that this is because of permission issues. Creating icons requires touching COM APIs, and doing that requires that the app has Full Trust. Full Trust is not a default for ClickOnce deployments and you should be very careful about using that option. [Brian has responded with a post with a very indepth and well-informed explanation of why the shortcuts are not an option and great insight to workarounds and security implementations.]

I found it amusing how obvious the lack of desktop shortcut creation is when looking at the “ClickOnce and Windows Installer Comparison Table“ in the MSDN ClickOnce overview documentation. The document compares the two deployment method's ability to add your application to the Start menu, to the Startup group and to the Favorites menu (“yes' “no” and “no“ for ClickOnce and “yes” “yes” and “yes”  for MSI) but they don't even list “Desktop Shortcut” as a feature for comparison.

Brian Noyes, who is the ClickOnce guru (with a whitepaper coming shortly on MSDN and an entire book on the subject coming out in October) suggested that the only way to do it is with custom install code. I expect Brian will come up with manifest or Deployment API code that runs once and only once. The downside to my solution is that it runs every single time you start up the application. But it will do for now and I will have a very happy camper at my client's office on Tuesday morning when he reads my email that I finally figured it out!. In October, when Brian's book comes out, I'll go for the more efficient solution!

I can't even take credit for my solution. That goes to Les Smith, who's article on Know Dot Net showed me the way. I tweaked his code a bit. Rather than republish his code here, I'll let you grab it from his article. Then you can use my changes if you wish.

  1. Changed IsShortcutExtant function to a Shared function named “Exists”
  2. Changed CreateShortCutOnDesktop function to a Shared function named “Create”.
  3. Changed parameters of Create to accept a string for shortcut name (with .lnk) and the actual text of the shortcut. Then I create those dynamically rather than hard code them. (Now I can drop this class into other applications!)
  4. Created a parameter for Exists that accepts a string to test for the .lnk name, and then modified that code to build the name dynamically.
  5. Les calls methods in WshShell without instantiating them. VB.NET 2005 complained that they were not shared functions. So I merely instantiated a new WshShell object call shell and made my calls from that.

Once I created a class from these two methods, Exists and Create, I just have to test for Exist and if it returns false, run Create. I do this at the very beginning of the app. You could combine the two into one method if you prefer.

Since the app is being deployed with ClickOnce, and it runs when it is first installed, this will happen right away.

Remember, this is only an option with Full Trust applications. Otherwise, you will get a SecurityException. I am using it for a custom written application that is only being installed on computers of my client's employees. The app already has full trust based on other requirements. (Don Kiely, Mr. Least Privilege, is having a cow right now if he is reading this.)

[NOTE AN IMPORTANT UPDATE TO THIS METHOD. SEE THIS POST FROM JULY 28]

posted on Sunday, May 28, 2006 9:38 PM