以编程方式创建回收站或其他特殊文件夹的快捷方式

Programmatically create a shortcut to the recycle bin or other special folders

我正在尝试制作一个将创建回收站快捷方式的控制台应用程序。

我的代码:

string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
object shDesktop = (object)"Desktop";
WshShell shell = new WshShell();
string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\Recycle Bin.lnk";
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
shortcut.Description = "New shortcut for Recycle Bin";
shortcut.Hotkey = "Ctrl+Shift+N";
shortcut.IconLocation = @"C:\WINDOWS\System32\imageres.dll";
shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\Recycle.Bin";
shortcut.Save();

它创建了一个 "Shortcut" 但它根本不可用。时弹出一条消息。我尝试打开它会产生:

"Windows is searching for recycle.bin. To locate your file yourself click browse."

如果要创建打开特殊文件夹的快捷方式,您需要创建 explorer.exe 和 pass the appropriate GUID 的快捷方式,并以双冒号作为参数前缀:

string explorerExePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "explorer.exe");
shortcut.TargetPath = explorerExePath;
shortcut.Arguments = "::{645FF040-5081-101B-9F08-00AA002F954E}";

您甚至不需要提供 explorer.exe 作为目标,您可以直接将 GUID 作为目标:

shortcut.TargetPath = "::{645FF040-5081-101B-9F08-00AA002F954E}";

或者,您可以 enable the display of the Recycle Bin on the desktop instead

指定回收站的特殊CLSID为TargetPath:

IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
shortcut.TargetPath = "::{645ff040-5081-101b-9f08-00aa002f954e}";
shortcut.Save();

也无需指定 IconLocation。如果是特殊文件夹,会自动选择合适的图标。