在c#中创建文件夹的快捷方式

2024-01-24

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

撇开歧义错误不谈:

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

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

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

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

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


不要忘记将嵌入互操作类型设置为 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();
    }

source: http://zayko.net/post/How-to-create-Windows-shortcut-(C).aspx http://zayko.net/post/How-to-create-Windows-shortcut-%28C%29.aspx

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在c#中创建文件夹的快捷方式 的相关文章

随机推荐