c# - c#创建文件夹快捷方式

标签 c# winforms

长话短说,我需要使用 C# 创建文件夹的快捷方式。我一直在阅读有关使用 IWshRuntimeLibrary 的内容。当我尝试使用 IWshRuntimeLibrary 进行任何操作时,我遇到了 System.IO.File 的各种歧义错误。我假设这是因为有 IWshRuntimeLibrary.File 接口(interface)和 System.IO.File。我真正能找到的只是关于制作应用程序快捷方式的文章,而不是文件夹。

撇开歧义错误:

  • 这是用于文件夹快捷方式的正确工具吗?
  • 如何使用它创建快捷方式
  • 如何指定放置快捷方式的位置

另外,当我尝试创建一个文件夹的快捷方式时,例如 C:\TEMP 使用这个:

IWshShortcut shortcut;
wshShell = new WshShellClass();
shortcut = (IWshShortcut)wshShell.CreateShortcut(@"C:\TEMP");

shortcut.TargetPath = @"C:\Documents and Settings";

我得到一个 COMException。根据我读到的内容,这应该创建一个指向 C 驱动器临时文件夹的快捷方式,并将该快捷方式放在“文档和设置”中。

最佳答案

不要忘记将 Embed Interop Types 设置为 False 以引用 Interop.IWshRuntimeLibrary。 我进行了测试并且没有出现错误。

  // Make sure you use try/catch block because your App may has no permissions on the target path!
  try
  {
    CreateShortcut(@"C:\temp", @"C:\MyShortcutFile.lnk",
        "Custom Shortcut", "/param", "Ctrl+F", @"c:\");
  }
  catch (Exception ex)
  {
    Console.WriteLine(ex.Message);
  }



   /// <summary>
    /// Create Windows Shorcut
    /// </summary>
    /// <param name="SourceFile">A file you want to make shortcut to</param>
    /// <param name="ShortcutFile">Path and shorcut file name including file extension (.lnk)</param>
    public static void CreateShortcut(string SourceFile, string ShortcutFile)
    {
      CreateShortcut(SourceFile, ShortcutFile, null, null, null, null);
    }

    /// <summary>
    /// Create Windows Shorcut
    /// </summary>
    /// <param name="SourceFile">A file you want to make shortcut to</param>
    /// <param name="ShortcutFile">Path and shorcut file name including file extension (.lnk)</param>
    /// <param name="Description">Shortcut description</param>
    /// <param name="Arguments">Command line arguments</param>
    /// <param name="HotKey">Shortcut hot key as a string, for example "Ctrl+F"</param>
    /// <param name="WorkingDirectory">"Start in" shorcut parameter</param>
    public static void CreateShortcut(string TargetPath, string ShortcutFile, string Description,
       string Arguments, string HotKey, string WorkingDirectory)
    {
      // Check necessary parameters first:
      if (String.IsNullOrEmpty(TargetPath))
        throw new ArgumentNullException("TargetPath");
      if (String.IsNullOrEmpty(ShortcutFile))
        throw new ArgumentNullException("ShortcutFile");

      // Create WshShellClass instance:
      var wshShell = new WshShellClass();

      // Create shortcut object:
      IWshRuntimeLibrary.IWshShortcut shorcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(ShortcutFile);

      // Assign shortcut properties:
      shorcut.TargetPath = TargetPath;
      shorcut.Description = Description;
      if (!String.IsNullOrEmpty(Arguments))
        shorcut.Arguments = Arguments;
      if (!String.IsNullOrEmpty(HotKey))
        shorcut.Hotkey = HotKey;
      if (!String.IsNullOrEmpty(WorkingDirectory))
        shorcut.WorkingDirectory = WorkingDirectory;

      // Save the shortcut:
      shorcut.Save();
    }

来源:http://zayko.net/post/How-to-create-Windows-shortcut-(C).aspx

关于c# - c#创建文件夹快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13019189/

相关文章:

c# - Unity3D : Cannot change scripting backend to . 网络

c# - 通过 RestSharp 发送非多部分数据

.net - C++/CLI 找不到资源

c# - 将字体转换为字符串并再次转换回来

c# - GUI 不在同一线程中更新 c# winforms

c# - 集合类型名称中的 '1 是什么

c# - 通过控制台界面访问编译器时包含库

c# - 将 C# lambda 函数转换为 VB.net

c# - 更改c#中菜单项附近箭头的颜色

vb.net - VB.NET 中的高级 GUI 设计