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 Desktop Shortcuts - A serious update to a previous post

Well, it turns out that my desktop icon for ClickOnce solution was more than cheesy, it was a total scam! Unintentional as it may have been. But this is what's on my lunch plate today.

What was I thinking? The funny (sad?) part is that I made this discovery myself and nobody ever pointed out the big flaw in this recent blog post "trick" that I wrote.

I was making a desktop shortcut to the application executable of the current version of the deployed app. There are two problems there. One should be obvious if I repeat "current version" . ClickOnce apps are versioned in separate folders. And I knew that when I came up with my hack. So even if I got an update, the shortcut would still be pointing to the original version. DUH! I had wondered why once in a while, a user would ask if whether or not I had made a promised (and already deployed) change to their app.(Thank goodness we are still in testing!)

But that's not even the big problem.

The real problem is that ClickOnce doesn't get engaged when you run directly from the application exe. So the update check doesn't even happen.

Surely you've noticed at some point in your experience as a computer user that when you install apps from an MSI and you get a shortcut, but the properties do not reveal the actual path of the exe. Take a look at the properties of a ClickOnce deployed app's shortcut in Start/Programs. It's a little different.

The file type is Application Reference and the location only points to C:\Documents and Settings\me_the_user\Start Menu\Programs\DesignatedFolderName. A circular reference. You can see a bit more if you make a new shortcut on the desktop and point into this location. Rather than getting a pointer to MyApp.exe, it will be MyApp.appref-ms.

So what is this file? If you open it up in notepad, you will see that it contains the URL to the installation manifest on the webserver along with a pointer to the manifest and more:

http://www.mydomain.com/Upates/MyApp.application#MyApp.application, Culture=neutral, PublicKeyToken=XXXXXXXXXXXX, processorArchitecture=msil

Saurabh Pants, from the ClickOnce team has explained this file in a post about deploying ClickOnce from FireFox.

Out of curiosity, I created a new text file on my desktop and filled it with the contents of my appref-ms file, but the location of the new shortcut confused ClickOnce to no end.

Back to the appref-ms shortcut. I got side-tracked big time by the fact that I hadn't been able to get my own icon to be tied to the shortcut in the Start/Programs menu, which meant the icon was a white box and so was the shortcut I was creating of the icon. Yucch!  Just today I finally found the solution to that buried in the MSDN Forums (thanks Isabelle!). It requires manually editing the application manifest every single time you do an update. Hopefully the next version of MAGE will give us a way to do this (as well as fixing the crash that happens when you update your file list.) The trick is to add inside of the tag of the application manifest file after it has been created, but before you sign it. That's good for those of us doing manual deployments.

Lastly is the new way of getting the icon on the the desktop automatically. The user's can create it myself, but my client has specifically requested that it be done.

In the applications startup (the first code the app hits when running) I do a check for IsFirstRun (which by default is true for each new version of the app...check the msdn docs for more details and gotchas on that). If thats true, then I just do a file copy, choosing the path of the shortcut in the program files menu (remember that this is per windows login) and then copying it to a path on the user's desktop.

Try
 
If  System.Deployment.Application.ApplicationDeployment.CurrentDeployment.IsFirstRun
Then
      copyShortcut("MyCompany", "MyApplication.appref-ms"
)
  End
If
Catch ex As
Deployment.Application.InvalidDeploymentException
'this will always occur in debug mode. Also if user tries to run from the actual EXE
   MessageBox.Show(
"Please run this application from the shortcut in the Start Menu to get the automatic update
   feature working."
& vbCrLf & vbCrLf &
"You can create a desktop shortcut from that particular shortcut if you
   like."
, messageboxtitle
)
Catch ex As
Exception
   'handle other exceptions as needed....
End Try

Public Shared Sub copyShortcut(ByVal manufacturerName As String, ByVal shortcutFileName As String)
  'the easy way for VB programmers *
   Dim desktopPath As String = My.Computer.FileSystem.SpecialDirectories.Desktop + "\"
+ shortcutFileName
  
Dim startPath As String = My.Computer.FileSystem.SpecialDirectories.Programs + "\" + manufacturerName + "\"
+ shortcutFileName
  
Try
     
System.IO.File.Copy(startPath, desktopPath, True
)
  
Catch e As
Exception
     
Throw
e
  
End
Try
End Sub

So after years and years, I finally have it all working. My client is ecstatic.I'm pretty happy. The deployment process for updating could be easier (watch for another post on this), but I can live with that for now.

*Scott Schecter translated the use of the special VB MY classes into C# here.

posted on Friday, July 28, 2006 11:59 AM