这里有一段代码,可以让你创建一个文件夹快捷方式。通常情况下,CoCreateInstance可以被用简单的类来替代,该类需要使用
Guid属性声明必需的CLSID以及
ComImport属性。使用
new
调用将自动实现COM操作。使用这段代码,您甚至不需要Shell32引用(或者如果您愿意,则可以重新使用那里的IShellLink声明)。
使用方法:
static void Main(string[] args)
{
CreateFolderShortcut(@"c:\temp", Path.GetFullPath("Shortcut to Temp"));
}
代码:
public static void CreateFolderShortcut(string path, string shortcutPath)
{
CreateFolderShortcut(path, shortcutPath, null);
}
public static void CreateFolderShortcut(string path, string shortcutPath, string comment)
{
if (path == null)
throw new ArgumentNullException("path");
IShellLink link = (IShellLink)new ShellLinkFolder();
if (comment != null)
{
link.SetDescription(comment);
}
link.SetPath(path);
IPersistFile file = (IPersistFile)link;
file.Save(shortcutPath, false);
}
[ComImport]
[Guid("00021401-0000-0000-C000-000000000046")]
private class ShellLink
{
}
[ComImport]
[Guid("0AFACED1-E828-11D1-9187-B532F1E9575D")]
private class ShellLinkFolder
{
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214F9-0000-0000-C000-000000000046")]
private interface IShellLink
{
void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out IntPtr pfd, int fFlags);
void GetIDList(out IntPtr ppidl);
void SetIDList(IntPtr pidl);
void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
void GetHotkey(out short pwHotkey);
void SetHotkey(short wHotkey);
void GetShowCmd(out int piShowCmd);
void SetShowCmd(int iShowCmd);
void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, int cchIconPath, out int piIcon);
void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
void Resolve(IntPtr hwnd, int fFlags);
void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
}