总而言之,我需要使用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";
我得到COMException
。根据我所读到的,这应该创建一个C驱动器临时文件夹的快捷方式,并将快捷方式放在文档和设置中。
答案 0 :(得分:7)
不要忘记将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
答案 1 :(得分:3)
你反而得到了:你试图让C:\Temp
成为C:\Documents and Settings
文件夹的快捷方式。
以下代码段将创建桌面上网络文件夹的快捷方式:
var desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
IWshShortcut shortcut;
var wshShell = new WshShellClass();
shortcut = (IWshShortcut)wshShell.CreateShortcut(Path.Combine(desktop, @"Temp.lnk"));
shortcut.TargetPath = @"\\computername\sharename";
shortcut.Save();
答案 2 :(得分:0)
在专家交流中使用IShellLink
的解决方案:
提供托管图层,供您创建文件和文件夹缩略图。
它是VB.NET,但您可以使用免费转换器将其转换为C#。