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

    Question

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

Answers

  • 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 7:29 PM
    Moderator
  • 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);

    }

    • Marked as answer by Harry Zhu Tuesday, October 07, 2008 3:07 AM
    Friday, October 03, 2008 8:30 PM

All replies

  • 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:10 PM
  • 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 7:29 PM
    Moderator
  • 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);

    }

    • Marked as answer by Harry Zhu Tuesday, October 07, 2008 3:07 AM
    Friday, October 03, 2008 8:30 PM
  • 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/
    Friday, October 03, 2008 8:45 PM
    Moderator
  • It's "nobugz" , not "aa".  Shame on you.

    Hans Passant.
    Thursday, June 18, 2009 10:15 AM
    Moderator
  • 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, April 18, 2011 7:05 PM
  • required name space : using IWshRuntimeLibrary;

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

    Monday, June 25, 2012 11:37 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

    Monday, June 25, 2012 11:49 AM