Solved

How to change icon on Desktop shortcut in vb.net

Posted on 2009-06-30
3
740 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 1

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 1

Author Closing Comment

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now