How to change the icon for a shortcut on the desktop in C# 2.0?
-
Friday, October 03, 2008 6:56 PMHi,
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 PMRight Click on Desktop==>Properties==>Change Icon.
Some file extensions do not allow to do it. Cs should be able to.
AlexB- Marked As Answer by Harry ZhuModerator Tuesday, October 07, 2008 3:07 AM
- Unmarked As Answer by nobugzMVP, Moderator Thursday, March 22, 2012 8:37 AM
-
Friday, October 03, 2008 7:29 PMModerator
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/- Edited by David M MortonModerator Friday, October 03, 2008 7:41 PM
- Proposed As Answer by David M MortonModerator Friday, October 03, 2008 8:58 PM
- Marked As Answer by Harry ZhuModerator Tuesday, October 07, 2008 3:06 AM
-
Friday, October 03, 2008 8:30 PM
Thanks for your suggestion. I use the followings and it works.
void appShortcutToDesktop(string name)
private{
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 codeShell32.
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 shortcutlnk.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 ZhuModerator Tuesday, October 07, 2008 3:07 AM
-
Friday, October 03, 2008 8:45 PMModerator
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 AMModerator
-
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 AMrequired 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

