【问题标题】:Programmatically create a shortcut to the recycle bin or other special folders以编程方式创建回收站或其他特殊文件夹的快捷方式
【发布时间】:2017-01-24 09:53:09
【问题描述】:

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

我的代码:

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();

它创建了一个“快捷方式”,但它根本不可用。时会弹出一条消息。我尝试打开它会产生:

“Windows 正在搜索 recycle.bin。要自行查找文件,请单击浏览。”

【问题讨论】:

  • 欢迎使用 Stack Overflow。请删除thanks in advance 部分。
  • 那么是什么让您认为回收站是一个名为Recycle.Bin 的独立文件?你知道 Windows 中有一个设置可以在桌面上显示回收站吗?
  • 我的回收站已经可见,但你的意思是什么?也许我误解了你的回答..
  • 我当然不认为 c:\windows\system32\recycle.bin 是正确的路径,我认为它始终是驱动器的根目录并称为 $recycle.bin
  • 这不是一个答案,而是一个问题。你想做什么?

标签: c# windows-shell desktop-shortcut


【解决方案1】:

指定回收站的特殊CLSID为TargetPath

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

也无需指定IconLocation。在特殊文件夹的情况下会自动选择适当的图标。

【讨论】:

    【解决方案2】:

    如果要创建打开特殊文件夹的快捷方式,则需要创建 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

    【讨论】:

    • 实际上没有必要使用explorer exe的链接。您可以直接使用特殊的 CLSID 作为TargetPath。看我的回答。
    • 非常感谢,非常感谢!
    猜你喜欢
    • 2015-10-04
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    • 2016-05-15
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多