FileUtil.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using IWshRuntimeLibrary;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. namespace GeekDesk.Util
  10. {
  11. public class FileUtil
  12. {
  13. private static readonly string NO_PATH = "{(.*)}";
  14. private static readonly string NO_ICO = "^,(.*)";
  15. private static readonly string HAVE_ICO = "(.*),(.*)";
  16. public static string GetTargetPathByLnk(string filePath)
  17. {
  18. try
  19. {
  20. WshShell shell = new WshShell();
  21. IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(filePath);
  22. if (StringUtil.IsEmpty(shortcut.TargetPath))
  23. {
  24. return null;
  25. }
  26. string path = shortcut.TargetPath;
  27. if (path == null || Regex.IsMatch(path, NO_PATH))
  28. {
  29. path = ParseShortcut(filePath);
  30. }
  31. return path;
  32. }
  33. catch (Exception e)
  34. {
  35. LogUtil.WriteErrorLog(e, "获取目标路径失败! filePath=" + filePath);
  36. return null;
  37. }
  38. }
  39. /// <summary>
  40. /// 获取启动参数
  41. /// </summary>
  42. /// <param name="filePath"></param>
  43. /// <returns></returns>
  44. public static string GetArgByLnk(string filePath)
  45. {
  46. try
  47. {
  48. WshShell shell = new WshShell();
  49. IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(filePath);
  50. return shortcut.Arguments;
  51. }
  52. catch (Exception e)
  53. {
  54. LogUtil.WriteErrorLog(e, "获取启动参数失败! filePath=" + filePath);
  55. return "";
  56. }
  57. }
  58. /// <summary>
  59. /// 获取iconpath
  60. /// </summary>
  61. /// <param name="filePath"></param>
  62. /// <returns></returns>
  63. public static string GetIconPathByLnk(string filePath)
  64. {
  65. try
  66. {
  67. WshShell shell = new WshShell();
  68. IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(filePath);
  69. var iconPath = shortcut.IconLocation;
  70. if (StringUtil.IsEmpty(iconPath)
  71. || Regex.IsMatch(iconPath, NO_ICO)
  72. || Regex.IsMatch(iconPath, NO_PATH)
  73. || !Regex.IsMatch(iconPath, HAVE_ICO))
  74. {
  75. return null;
  76. }
  77. else
  78. {
  79. return iconPath.Split(',')[0];
  80. }
  81. }
  82. catch (Exception e)
  83. {
  84. LogUtil.WriteErrorLog(e, "获取图标路径失败! filePath=" + filePath);
  85. return null;
  86. }
  87. }
  88. /*
  89. UINT MsiGetShortcutTarget(
  90. LPCTSTR szShortcutTarget,
  91. LPTSTR szProductCode,
  92. LPTSTR szFeatureId,
  93. LPTSTR szComponentCode
  94. );
  95. */
  96. [DllImport("msi.dll", CharSet = CharSet.Auto)]
  97. static extern int MsiGetShortcutTarget(string targetFile, StringBuilder productCode, StringBuilder featureID, StringBuilder componentCode);
  98. public enum InstallState
  99. {
  100. NotUsed = -7,
  101. BadConfig = -6,
  102. Incomplete = -5,
  103. SourceAbsent = -4,
  104. MoreData = -3,
  105. InvalidArg = -2,
  106. Unknown = -1,
  107. Broken = 0,
  108. Advertised = 1,
  109. Removed = 1,
  110. Absent = 2,
  111. Local = 3,
  112. Source = 4,
  113. Default = 5
  114. }
  115. public const int MaxFeatureLength = 38;
  116. public const int MaxGuidLength = 38;
  117. public const int MaxPathLength = 1024;
  118. /*
  119. INSTALLSTATE MsiGetComponentPath(
  120. LPCTSTR szProduct,
  121. LPCTSTR szComponent,
  122. LPTSTR lpPathBuf,
  123. DWORD* pcchBuf
  124. );
  125. */
  126. [DllImport("msi.dll", CharSet = CharSet.Auto)]
  127. static extern InstallState MsiGetComponentPath(string productCode, string componentCode, StringBuilder componentPath, ref int componentPathBufferSize);
  128. public static string ParseShortcut(string file)
  129. {
  130. StringBuilder product = new StringBuilder(MaxGuidLength + 1);
  131. StringBuilder feature = new StringBuilder(MaxFeatureLength + 1);
  132. StringBuilder component = new StringBuilder(MaxGuidLength + 1);
  133. MsiGetShortcutTarget(file, product, feature, component);
  134. int pathLength = MaxPathLength;
  135. StringBuilder path = new StringBuilder(pathLength);
  136. InstallState installState = MsiGetComponentPath(product.ToString(), component.ToString(), path, ref pathLength);
  137. if (installState == InstallState.Local)
  138. {
  139. return path.ToString();
  140. }
  141. else
  142. {
  143. return null;
  144. }
  145. }
  146. }
  147. }