Solved

How to change icon on Desktop shortcut in vb.net

Posted on 2009-06-30
3
764 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net datagrid point 4 33
CHANGE FORM TEXT BEFORE IT SHOW TO USER 13 32
Adjust the codes 3 63
Need Help With GDI+ in VB.Net 8 39
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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