我需要测试文件是否是快捷方式.我仍在试图弄清楚将如何设置内容,但我可能只有它的路径,我可能只有文件的实际内容(作为byte [])或者我可能同时拥有它们.
一些复杂性包括我可以在一个zip文件中(在这种情况下,路径将是一个内部路径)
可以使用SHELL32.DLL中的COM对象来操作快捷方式.
在Visual Studio项目中,添加对COM库"Microsoft Shell Controls And Automation"的引用,然后使用以下命令:
////// Returns whether the given path/file is a link /// /// ///public static bool IsLink(string shortcutFilename) { string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename); string filenameOnly = System.IO.Path.GetFileName(shortcutFilename); Shell32.Shell shell = new Shell32.ShellClass(); Shell32.Folder folder = shell.NameSpace(pathOnly); Shell32.FolderItem folderItem = folder.ParseName(filenameOnly); if (folderItem != null) { return folderItem.IsLink; } return false; // not found }
您可以按如下方式获取链接的实际目标:
////// If path/file is a link returns the full pathname of the target, /// Else return the original pathnameo "" if the file/path can't be found /// /// ///public static string GetShortcutTarget(string shortcutFilename) { string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename); string filenameOnly = System.IO.Path.GetFileName(shortcutFilename); Shell32.Shell shell = new Shell32.ShellClass(); Shell32.Folder folder = shell.NameSpace(pathOnly); Shell32.FolderItem folderItem = folder.ParseName(filenameOnly); if (folderItem != null) { if (folderItem.IsLink) { Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink; return link.Path; } return shortcutFilename; } return ""; // not found }