views:

2327

answers:

4

Hi,

How to remove a shortcut file in c#,how to remove the program icon from the Programs folder?.

Redards, Harsh Suman

+1  A: 

You can use the standard file operations on shortcuts.

I believe the file extension is lnk.

Guvante
+3  A: 

A shortcut file is a normal file that happens to redirect (on click) the call to another file, program or directory. To remove a shortcut you can use the File.Delete method.

File.Delete(path_to_lnk_file);
smink
+2  A: 

To get the start menu location, use the SpecialFolder enumeration. Something like the following should get you started:

string startMenuDir = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
string shortcut = Path.Combine(startMenuDir, @"The Company\MyShortcut.lnk");
if (File.Exists(shortcut))
    File.Delete(shortcut);

If you don't know the exact file name, you could enumerate over all the files in the start menu folder using Directory.GetFiles or Directory.GetDirectories. You could also remove the entire folder ("The Company"), using Directory.Delete

Isak Savo
+1  A: 

In Windows explorer, the file extension for links (lnk) is never shown, even if you have disabled the Hide extensions for known file types feature.

So if you want to delete the 'Shortcut to foobar.exe' shortcut, you have to do

File.Delete("Shortcut to foobar.exe.lnk");
Treb