Solved

How to change icon on Desktop shortcut in vb.net

Posted on 2009-06-30
3
758 Views
Last Modified: 2012-06-27
I need to create and change the icon on a desktop shortcut to an ico file on the C drive, using vb.net.   I have tried the code below and have registered MS Shell Controls and Automation but still get "Not Declared" errors.   Is there a .net way to do this, or how do I get the code below to run??
objShell = WScript.CreateObject("WScript.Shell")
 strDesktopFld = objShell.SpecialFolders("Desktop")
 objURLShortcut = objShell.CreateShortcut(strDesktopFld & "\blah.url")
 objURLShortcut.TargetPath = "http://www.blah.com"
 objURLShortcut.IconLocation("\\server\share\icon.ico ")
 objURLShortcut.Save()

Open in new window

0
Comment
Question by:PNRT
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
carlnorrbom earned 500 total points
ID: 24743358
Hi,

To start off with, you must include a Reference to the Windows Scripting Library.  You do this by right-clicking on the References folder in the Solution Explorer.  Select the Add Reference menu.  From the Add Reference dialog, click the Com Tab and select the Windows Scripting Host Object Model and click the Add button.

Then add an imports statement:

Imports IWshRuntimeLibrary

Then review the attached code as an example.

/Carl.
Public Function CreateShortCutOnDesktop() As Boolean
        Try
            Dim objShell As New WshShell()
            Dim desktopdir As String = CType(objShell.SpecialFolders.Item("Desktop"), String)
            Dim shortCut As WshShortcut = objShell.CreateShortcut(desktopdir & "\Blah.lnk")
            With shortCut
                .TargetPath = "http://www.blah.com"
                .WindowStyle = 1
                .Description = "Visit blah.com"
                .WorkingDirectory = desktopdir
                .IconLocation = "\\server\share\icon.ico"
                .Save()
            End With
            Return True
        Catch ex As Exception
            'errorhandling goes here
            Return False
        End Try
   End Function

Open in new window

0
 
LVL 2

Author Comment

by:PNRT
ID: 24743645
Hi - Thanks for this - it worked first time in my test app, but when I addedd to the main program all my
"If File.Exists(path_file) Then" - about 30 of them - all give the exception

'File' is ambiguous, imported from the namespaces or types 'IWshRuntimeLibrary, System.IO'.

Is there a way round this?  I cant really redo the File.Exists.

Thanks for your help
0
 
LVL 2

Author Closing Comment

by:PNRT
ID: 31598261
Hi - sorted, adding system.io to each got it working
Thanks for your help
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question