Getting ClickOnce to create a desktop shortcut
I am using VS2005 Beta2. But I have not found a way to tap this feature. How do I set ClickOnce to create a shortcut on a user's desktop during installation?
Does anyone know?
I am using VS2005 Beta2. But I have not found a way to tap this feature. How do I set ClickOnce to create a shortcut on a user's desktop during installation?
Does anyone know?
Here is something that I wrote that does what Click Once do - verifies if there is an updated version then Get the latest version and then create a shortcut on the desktop.
User starts the Myapp - application verifies version - if change then - opens up setup and closes MyApp - Setup updates all the files - starts MyApp and closes itself.
The only thing I need to do it that I need to put the latest build on the server (as click once does)
Also currently I am storing the latest version in database so that everytime I start MyApp the prog compares the version of MyApp and database field - if different it runs the Setup program - but you could also store the same in an xml file which you need to update with every build - you could do that programatically and then put it in the same folder where the build is so that Setup program copies this xml file also to the client machine and then you can comapre these 2 files.
Code changes in MyApp
'this could be in Form load event or on application start up
Dim fv As FileVersionInfo
fv = System.Diagnostics.FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location)
if fv.FileVersion <> sversion then
System.Diagnostics.Process.Start("\\Server3\procs\DotNet\MyAppSetup\Setup.exe")
Me.Close()
end if
Code for Setup program
Dim srcPath As String = "\\Server3\procs\DotNet\MyAppOutput"
Dim TgtPath As String = "C:\Program Files\CompanyName\MyApp"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call copyFiles()
System.Diagnostics.Process.Start("C:\Program Files\CompanyName\MyApp.exe")
Me.Close()
End Sub
Private Function IsValidPath(ByVal sPath As String) As Boolean
Dim bret As Boolean = False
Try
If Directory.Exists(sPath) Then
bret = True
Else
Directory.CreateDirectory(sPath)
End If
Catch ex As Exception
End Try
Return bret
End Function
Private Sub copyFiles()
Dim sFile As String
'copy all files from src to target
Try
Call IsValidPath(TgtPath)
For Each sFile In Directory.GetFiles(SrcPath)
File.Copy(sFile, TgtPath & "/" & sFile.Substring(sFile.LastIndexOf("\") + 1), True)
Next
Catch ex As Exception
MsgBox("Please contact the administartor" & Chr(13) & ex.Message)
End Try
Shortcut.CreateShortCutOnDesktop(TgtPath & "\" & "MachineStatus.exe")
End Sub
'Include the com object - Windows Script Host object Model in the references
Imports IWshRuntimeLibrary
Imports System.IO
Public Module Shortcut
Private TheShell As New IWshRuntimeLibrary.IWshShell_Class()
Public Sub CreateShortcut(ByVal linkFilename As String, ByVal description As String, ByVal targetPath As String, ByVal iconLocation As String, ByVal iconIndex As Integer) '
'ToDo: Error processing original source shown below
'
'
'-^ Syntax error: 'identifier' expected
Dim shortcut As IWshShortcut_Class = CType(New IWshShell_ClassClass().CreateShortcut(linkFilename), IWshShortcut_Class)
shortcut.Description = description
shortcut.TargetPath = targetPath
shortcut.IconLocation = iconLocation + "," + iconIndex
shortcut.Save()
End Sub 'CreateShortcut
Public Function CreateShortCutOnDesktop(ByVal SrcPath As String) As Boolean
Try
Dim DesktopDir As String = _
CType(TheShell.SpecialFolders.Item("Desktop"), String)
Dim shortCut As IWshRuntimeLibrary.IWshShortcut
' short cut files have a .lnk extension
shortCut = CType(TheShell.CreateShortcut(DesktopDir & _
"\MyApp.lnk"), _
IWshRuntimeLibrary.IWshShortcut)
' set the shortcut properties
With shortCut
.TargetPath = SrcPath
.WindowStyle = 1
.Description = "MyApp"
.WorkingDirectory = DesktopDir
.IconLocation = _
System.Reflection.Assembly.GetExecutingAssembly.Location() & _
", 0"
.Save() ' save the shortcut file
End With
Return True
Catch ex As System.Exception
Return False
End Try
End Function
End Module
I don't think I want to stray from ClickOnce though, I am trying to move more and more of my program's operations to managed code processes to save time and effort.
But thank you.
The shortcuts referred to in the documentation are the Start Menu shortcuts that ClickOnce creates for installed applications. This release of ClickOnce does not support creation of desktop shortcuts, this is something we will look at for the next release.
After deploying application user can create desktop shortcut by drag-dropping copy of Start Menu shortcut onto desktop. Unfortunately there is no way to get ClickOnce to do this by default at install time.
If you are using ClickOnce I would not recommend using custom update logic that does not go through System.Deployment.Application APIs.
Regards,
Sameer
I put the following in a class and have it run when the app. first starts. So far, it's been working well. The only minor issues I can see are: 1)If the user renames the desktop shortcut, a new will be created the next time the program starts, and 2)If the program is uninstalled, the desktop shortcut would have to be removed manually.
Dim strShortcutName As String = Application.ProductName & ".appref-ms"
Dim strDesktopPath As String = _ "C:\Documents and Settings\" & _System.Environment.UserName & _
"\Desktop\" Dim strProgramMenuPath As String = _ "C:\Documents and Settings\" & _System.Environment.UserName & _
"\Start Menu\Programs\CompanyName\" ' If Dir(strProgramMenuPath & strShortcutName) = strShortcutName Then If Dir(strDesktopPath & strShortcutName) = strShortcutName Then ' Desktop Shortcut exists, do nothing Else TryFileCopy(strProgramMenuPath & strShortcutName, strDesktopPath & strShortcutName)
Catch ex As ExceptionMsgBox(ex.ToString)
End Try End If ElseMsgBox(
"Can't find application shortcut on the Program Menu.") End If ' ************************************************************************************