Solved

How to change icon on Desktop shortcut in vb.net

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

774 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