gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:27:28 24 4
gpt4 key购买 nike

长话短说,我需要使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com