Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to change icon on Desktop shortcut in vb.net

Posted on 2009-06-30
3
Medium Priority
?
782 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 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

604 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