How to change the icon for a shortcut on the desktop in C# 2.0?

Answered How to change the icon for a shortcut on the desktop in C# 2.0?

  • Friday, October 03, 2008 6:56 PM
     
     
    Hi,
    I would like to change the icon for a shortcut on the desktop in C# 2.0. How can I do that? Thanks.

All Replies

  • Friday, October 03, 2008 7:10 PM
     
     
    Right Click on Desktop==>Properties==>Change Icon.

    Some file extensions do not allow to do it. Cs should be able to.
    AlexB
  • Friday, October 03, 2008 7:29 PM
    Moderator
     
     Answered Has Code
    You have to use Windows Scripting Host to do this.  Here's how:

    1. Add your reference to IWshRuntimeLibrary:

    • Right click on references in your project. 
    • Click the "Browse" tab. 
    • Browse to C:\windows\system32\wshom.ocx, and select it.
    • This should add a reference in your application to "IWshRuntimeLibrary".


    2. Add a using statement to the top of your code file:

    • using IWshRuntimeLibrary;

    3. Add your code like this, replacing the shortcutLocation value with your own shortcut, and the newIconLocation value with your own value. The comma and the number after the assembly name refers to the icon's placement in the file.  In this situation, it's the exclamation mark in the yellow triangle.

    string shortcutLocation = @"C:\Documents and Settings\All Users\Desktop\Zune.lnk";  
    string newIconLocation = @"C:\Windows\System32\user32.dll,1";  
     
    WshShell Shell = new WshShellClass();  
    WshShortcut cl = (WshShortcut)oShell.CreateShortcut(shortcutLocation);  
    cl.IconLocation = newIconLocation;  
    cl.Save(); 


    David Morton - http://blog.davemorton.net/
  • Friday, October 03, 2008 8:30 PM
     
     Answered Has Code

    Thanks for your suggestion. I use the followings and it works.

    private
    void appShortcutToDesktop(string name)

    {

    String path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

    Shell32.Shell shl = new Shell32.ShellClass();

    // Optional code to create the shortcut

    if (File.Exists(path + @"\" + name + ".lnk"))

    {

    File.Delete(path + @"\" + name + ".lnk");

    }

    System.IO.StreamWriter sw = new System.IO.StreamWriter(path + @"\" + name + ".lnk", false);

    sw.Close();

    // End optional code

    Shell32.Folder dir = shl.NameSpace(path);

    Shell32.FolderItem itm = dir.Items().Item(name + ".lnk");

    Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)itm.GetLink;

    // Optional code to create the shortcut

    lnk.Path = @"a.exe";

    lnk.Description = "aa";

    lnk.WorkingDirectory = @"c:\";

    // End optional code

    File.Copy(Path.Combine(Application.StartupPath.ToString(), @"a.ico"), Environment.GetFolderPath(Environment.SpecialFolder.System) + "a.ico", true);

    lnk.SetIconLocation(Environment.GetFolderPath(Environment.SpecialFolder.System) + "a.ico", 0);

    lnk.Save(null);

    }

  • Friday, October 03, 2008 8:45 PM
    Moderator
     
      Has Code
    Yes, but doesnt:

    string shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "MyShortcut.lnk");  
    WshShell Shell = new WshShellClass();  
    WshShortcut shortcut = (WshShortcut)Shell.CreateShortcut(shortcutPath);  
    shortcut.TargetPath = @"C:\sol.exe";  
    shortcut.IconLocation = @"C:\Windows\System32\user32.dll,2";  
    shortcut.Save(); 

    look so much cleaner and easier to maintain?
    David Morton - http://blog.davemorton.net/
  • Thursday, June 18, 2009 10:15 AM
    Moderator
     
     
    It's "nobugz" , not "aa".  Shame on you.

    Hans Passant.
  • Monday, April 18, 2011 7:05 PM
     
     

    pleaes tell me:

    ShellClass, Shell .... are in which namespace, if some one doesn't know how to create shortcut, absolutely doesn't know what is Shell and its namespace.

    please help.

  • Monday, June 25, 2012 11:37 AM
     
     
    required name space : using IWshRuntimeLibrary;

    Thanks & Regards NVM Prasad * prasadforum@hotmail.com Skype ID: nvm.groups

  • Monday, June 25, 2012 11:49 AM
     
     

            Hi,

                Check below code its work....

    private WshShellClass WshShell;

            public void CreateFileShortcutIcon(string filepath)
            {
                string FilenameShotcut = string.Empty;
                try
                {
                    WshShellClass WshShell = new WshShellClass();
                    IWshRuntimeLibrary.IWshShortcut MyShortcut;
                    FilenameShotcut = filepath + ".lnk";
                    if (!System.IO.File.Exists(FilenameShotcut))
                    {
                        MyShortcut = (IWshRuntimeLibrary.IWshShortcut)WshShell.CreateShortcut(FilenameShotcut);
                        MyShortcut.TargetPath = filepath;
                        MyShortcut.Description = "Launch My Application";
                        MyShortcut.Save();
                    }
                }
                catch (Exception)
                {

                    throw;
                }
            }

              


    Thanks & Regards NVM Prasad * prasadforum@hotmail.com Skype ID: nvm.groups