none
Modifying desktop shortcut RRS feed

  • Question

  • Hi

    How can I find, from among the shortcuts on public desktop, the shortcut that has 'MyApp' in its name and change its icon?

    Thanks

    Regards


    • Edited by Y a h y a Wednesday, June 7, 2017 1:36 PM
    Wednesday, June 7, 2017 1:29 PM

Answers

  •  Perhaps this will work for you.  This example assumes that you want to use an icon file located on the hard drive.  It will find any (.lnk) shortcut file located on the desktop that has "MyApp" in its name and change the icon to the one you specify.

     It requires you to add a reference to (Windows Script Host Object Model).  To do that,  go to the Menu and click (Project) and then select (Add Reference).  On the Reference Manager window select the COM tab,  that is where you will find it.  Shown in the image below...

     

    Imports IWshRuntimeLibrary 'must add a reference to (Windows Script Host Object Model) in the (Add Reference) dialog window

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim DesktopPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            Dim scShell As New WshShell()
            For Each fn As String In IO.Directory.GetFiles(DesktopPath, "*.lnk").Where(Function(x) IO.Path.GetFileName(x).Contains("MyApp"))
                Dim Shortcut As WshShortcut = CType(scShell.CreateShortcut(fn), WshShortcut)
                Shortcut.IconLocation = "C:\TestFolder\MyIcon.ico" 'a path to the icon file to use
                Shortcut.Save()
                System.Runtime.InteropServices.Marshal.ReleaseComObject(Shortcut)
            Next
            System.Runtime.InteropServices.Marshal.ReleaseComObject(scShell)
        End Sub
    End Class

     

     

     

     

    EDIT:

     I missed that you wanted to do this for shortcuts in the Public Desktop folder.  That directory path can be retrieved using the SprcialFolder.CommonDesktopDirectory as shown below.  However,  if your shortcut is actually in that folder,  you will need admin rights to change the icon of the shortcut.  That means you will need to change your app.manifest to require them.  Although,  it is not a good idea to have your app run with admin rights at all times.

     In the app.manifest, you can change this line...

    <requestedExecutionLevel  level="asInvoker" uiAccess="false" />

     To this....

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
     

     Then rebuild your application.  You can get the Public desktop directory path instead of the current users Desktop path like shown below.

            Dim PublicDesktopPath As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory) '(Public Desktop Folder) admin rights are required

     


    If you say it can`t be done then i`ll try it

    Wednesday, June 7, 2017 4:04 PM

All replies

  • Wednesday, June 7, 2017 3:10 PM
  •  Perhaps this will work for you.  This example assumes that you want to use an icon file located on the hard drive.  It will find any (.lnk) shortcut file located on the desktop that has "MyApp" in its name and change the icon to the one you specify.

     It requires you to add a reference to (Windows Script Host Object Model).  To do that,  go to the Menu and click (Project) and then select (Add Reference).  On the Reference Manager window select the COM tab,  that is where you will find it.  Shown in the image below...

     

    Imports IWshRuntimeLibrary 'must add a reference to (Windows Script Host Object Model) in the (Add Reference) dialog window

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim DesktopPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            Dim scShell As New WshShell()
            For Each fn As String In IO.Directory.GetFiles(DesktopPath, "*.lnk").Where(Function(x) IO.Path.GetFileName(x).Contains("MyApp"))
                Dim Shortcut As WshShortcut = CType(scShell.CreateShortcut(fn), WshShortcut)
                Shortcut.IconLocation = "C:\TestFolder\MyIcon.ico" 'a path to the icon file to use
                Shortcut.Save()
                System.Runtime.InteropServices.Marshal.ReleaseComObject(Shortcut)
            Next
            System.Runtime.InteropServices.Marshal.ReleaseComObject(scShell)
        End Sub
    End Class

     

     

     

     

    EDIT:

     I missed that you wanted to do this for shortcuts in the Public Desktop folder.  That directory path can be retrieved using the SprcialFolder.CommonDesktopDirectory as shown below.  However,  if your shortcut is actually in that folder,  you will need admin rights to change the icon of the shortcut.  That means you will need to change your app.manifest to require them.  Although,  it is not a good idea to have your app run with admin rights at all times.

     In the app.manifest, you can change this line...

    <requestedExecutionLevel  level="asInvoker" uiAccess="false" />

     To this....

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
     

     Then rebuild your application.  You can get the Public desktop directory path instead of the current users Desktop path like shown below.

            Dim PublicDesktopPath As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory) '(Public Desktop Folder) admin rights are required

     


    If you say it can`t be done then i`ll try it

    Wednesday, June 7, 2017 4:04 PM