Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programatically delete shortcut from user's desktop?

Using C#, how can I delete a shortcut from a user's desktop?

Tried this with no success:

string WinUser = WindowsIdentity.GetCurrent().Name;
WinUser = WinUser.Substring(WinUser.LastIndexOf("\\") + 1);

File.Delete("C:\\Users\\" + WinUser + "\\Desktop\\Touch Data.lnk");

What am I missing? Appreciate any advice on this!

like image 646
kogh Avatar asked Feb 08 '12 14:02

kogh


People also ask

How do I remove a shortcut from all Users desktop?

Universal desktop program icon/shortcuts are located in C:\Users\Public\Desktop. To work around this, go into C:\Users\Public\Desktop and delete all the program icons.

How do I force Delete a shortcut?

First, I would suggest you to try deleting the shortcut by pressing delete key on keyboard, after clicking the desktop shortcut which you want to delete. Check if you are able to delete. Method 2: Check if you are able to delete these desktop shortcuts in safe mode.


2 Answers

Try the following:

string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
File.Delete(Path.Combine(desktopPath, "Touch Data.lnk"));
like image 170
Rich O'Kelly Avatar answered Sep 28 '22 08:09

Rich O'Kelly


I had this issue in this question I asked:

Why does FolderBrowserDialog not allow the desktop as SelectedPath when RootFolder is MyComputer?

The answer I got was this:

Apparently, the Desktop in Win 7 doesn't actually exist at the path

c:\Users\username\Desktop

The system pretends it does at the command prompt and in windows explorer. But since it isn't there, the part of SelectedPath that requires its path to be under RootFolder disallows setting the path in that way.

It's possible this is the issue. You should use the Environment.GetFolderPath function to get a handle on the real desktop. :)

like image 36
Almo Avatar answered Sep 28 '22 08:09

Almo