总而言之,我需要使用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";
Run Code Online (Sandbox Code Playgroud)
我得到了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();
}
Run Code Online (Sandbox Code Playgroud)
来源:http://zayko.net/post/How-to-create-Windows-shortcut-(C).aspx