标签:
身为码农,干活时会经常用到各种工具,为了提高搬砖效率,我一直用Executor作快速启动工具,想开什么程序直接啪啪啪敲几下键盘,逼格十足,后来get到个新技能:建一个文件夹存放常用程序的快捷方式,然后把这个文件夹的路径加到PATH系统环境变量中,就可以用直接用“Win + R”打开常用程序了。缺点是不便管理,许多程序被删了,但快捷方式还在,造成很多无效快捷方式,所以就想写个程序来自动删除无效的快捷方式,但没找到现成的解析快捷方式文件的库,看来只能自己造轮子了。
MSDN上有关于此文件格式的详细文档。文件中信息很多,不过大多数都用不到,不想花时间折腾了,只解析了三个:目标路径、是否是目录和快捷键。
主要就是这个类,比较简单,没几行代码:
1 public class WinShortcut 2 { 3 public string TargetPath { get; private set; } 4 public bool IsDirectory { get; private set; } 5 6 string _hotKey; 7 public string HotKey { get { return String.IsNullOrWhiteSpace(_hotKey) ? "无" : _hotKey; } private set { _hotKey = value; } } 8 9 public WinShortcut(string path) 10 { 11 Parse(path); 12 } 13 14 public override string ToString() 15 { 16 return string.Format("目标路径:\"{0}\",是否是文件夹:{1},快捷键:\"{2}\"", TargetPath, IsDirectory, HotKey); 17 } 18 19 void Parse(String shortcutPath) 20 { 21 using (var fs = new FileStream(shortcutPath, FileMode.Open, FileAccess.Read)) 22 { 23 fs.Seek(20, SeekOrigin.Begin); 24 var data = new byte[4]; 25 fs.Read(data, 0, data.Length); 26 var linkFlags = BitConverter.ToInt32(data, 0); 27 28 fs.Read(data, 0, data.Length); 29 var fileAttrFlags = BitConverter.ToInt32(data, 0); 30 IsDirectory = (fileAttrFlags & WinConsts.FileAttributes.Directory) == WinConsts.FileAttributes.Directory; 31 32 fs.Seek(36, SeekOrigin.Current); 33 fs.Read(data, 2, 2); 34 var hotKey = new List<string>(4); 35 var hotKeyLowByte = (ushort)data[2]; 36 var hotKeyHighByte = (ushort)data[3]; 37 if ((hotKeyHighByte & (ushort)WinConsts.VirtualKeys.HOTKEYF_CONTROL) == (ushort)WinConsts.VirtualKeys.HOTKEYF_CONTROL) 38 hotKey.Add("ctrl"); 39 if ((hotKeyHighByte & (ushort)WinConsts.VirtualKeys.HOTKEYF_SHIFT) == (ushort)WinConsts.VirtualKeys.HOTKEYF_SHIFT) 40 hotKey.Add("shift"); 41 if ((hotKeyHighByte & (ushort)WinConsts.VirtualKeys.HOTKEYF_ALT) == (ushort)WinConsts.VirtualKeys.HOTKEYF_ALT) 42 hotKey.Add("alt"); 43 if (Enum.IsDefined(typeof(WinConsts.VirtualKeys), hotKeyLowByte)) 44 { 45 hotKey.Add(((WinConsts.VirtualKeys)hotKeyLowByte).ToString()); 46 } 47 HotKey = String.Join(" + ", hotKey); 48 49 fs.Seek(0x4c, SeekOrigin.Begin); 50 if ((linkFlags & WinConsts.LinkFlags.HasLinkTargetIdList) == WinConsts.LinkFlags.HasLinkTargetIdList) 51 { 52 var buffer = new byte[2]; 53 fs.Read(buffer, 0, buffer.Length); 54 var size = BitConverter.ToInt16(buffer, 0); 55 fs.Seek(size, SeekOrigin.Current); 56 } 57 if ((linkFlags & WinConsts.LinkFlags.HasLinkInfo) == WinConsts.LinkFlags.HasLinkInfo) 58 { 59 var start = fs.Position;//记下LinkInfo的起始位置 60 fs.Seek(8, SeekOrigin.Current); 61 var buffer = new byte[4]; 62 fs.Read(buffer, 0, buffer.Length); 63 var lnkInfoFlags = BitConverter.ToInt32(buffer, 0); 64 if ((lnkInfoFlags & WinConsts.LinkInfoFlags.VolumeIDAndLocalBasePath) == WinConsts.LinkInfoFlags.VolumeIDAndLocalBasePath) 65 { 66 fs.Seek(4, SeekOrigin.Current); 67 fs.Read(buffer, 0, buffer.Length); 68 var localBasePathOffset = BitConverter.ToInt32(buffer, 0); 69 var basePathOffset = start + localBasePathOffset; 70 var length = 0; 71 fs.Seek(basePathOffset, SeekOrigin.Begin); 72 while (fs.ReadByte() > 0) 73 { 74 ++length; 75 } 76 var basePathRaw = new byte[length]; 77 fs.Seek(basePathOffset, SeekOrigin.Begin); 78 fs.Read(basePathRaw, 0, basePathRaw.Length); 79 TargetPath = Encoding.Default.GetString(basePathRaw); 80 } 81 } 82 } 83 } 84 }
标签:
原文地址:http://www.cnblogs.com/taney/p/4512257.html