FileUtil.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using IWshRuntimeLibrary;
  2. using System;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. namespace GeekDesk.Util
  8. {
  9. public class FileUtil
  10. {
  11. private static readonly string NO_PATH = "{(.*)}";
  12. private static readonly string NO_ICO = "^,(.*)";
  13. private static readonly string HAVE_ICO = "(.*),(.*)";
  14. public static string GetTargetPathByLnk(string filePath)
  15. {
  16. try
  17. {
  18. WshShell shell = new WshShell();
  19. IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(filePath);
  20. if (StringUtil.IsEmpty(shortcut.TargetPath))
  21. {
  22. return null;
  23. }
  24. string path = shortcut.TargetPath;
  25. if (path == null || Regex.IsMatch(path, NO_PATH))
  26. {
  27. path = ParseShortcut(filePath);
  28. }
  29. return path;
  30. }
  31. catch (Exception e)
  32. {
  33. LogUtil.WriteErrorLog(e, "获取目标路径失败! filePath=" + filePath);
  34. return null;
  35. }
  36. }
  37. /// <summary>
  38. /// 获取启动参数
  39. /// </summary>
  40. /// <param name="filePath"></param>
  41. /// <returns></returns>
  42. public static string GetArgByLnk(string filePath)
  43. {
  44. try
  45. {
  46. WshShell shell = new WshShell();
  47. IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(filePath);
  48. return shortcut.Arguments;
  49. }
  50. catch (Exception e)
  51. {
  52. LogUtil.WriteErrorLog(e, "获取启动参数失败! filePath=" + filePath);
  53. return "";
  54. }
  55. }
  56. /// <summary>
  57. /// 获取iconpath
  58. /// </summary>
  59. /// <param name="filePath"></param>
  60. /// <returns></returns>
  61. public static string GetIconPathByLnk(string filePath)
  62. {
  63. try
  64. {
  65. WshShell shell = new WshShell();
  66. IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(filePath);
  67. var iconPath = shortcut.IconLocation;
  68. if (StringUtil.IsEmpty(iconPath)
  69. || Regex.IsMatch(iconPath, NO_ICO)
  70. || Regex.IsMatch(iconPath, NO_PATH)
  71. || !Regex.IsMatch(iconPath, HAVE_ICO))
  72. {
  73. return null;
  74. }
  75. else
  76. {
  77. return iconPath.Split(',')[0];
  78. }
  79. }
  80. catch (Exception e)
  81. {
  82. LogUtil.WriteErrorLog(e, "获取图标路径失败! filePath=" + filePath);
  83. return null;
  84. }
  85. }
  86. /*
  87. UINT MsiGetShortcutTarget(
  88. LPCTSTR szShortcutTarget,
  89. LPTSTR szProductCode,
  90. LPTSTR szFeatureId,
  91. LPTSTR szComponentCode
  92. );
  93. */
  94. [DllImport("msi.dll", CharSet = CharSet.Auto)]
  95. static extern int MsiGetShortcutTarget(string targetFile, StringBuilder productCode, StringBuilder featureID, StringBuilder componentCode);
  96. public enum InstallState
  97. {
  98. NotUsed = -7,
  99. BadConfig = -6,
  100. Incomplete = -5,
  101. SourceAbsent = -4,
  102. MoreData = -3,
  103. InvalidArg = -2,
  104. Unknown = -1,
  105. Broken = 0,
  106. Advertised = 1,
  107. Removed = 1,
  108. Absent = 2,
  109. Local = 3,
  110. Source = 4,
  111. Default = 5
  112. }
  113. public const int MaxFeatureLength = 38;
  114. public const int MaxGuidLength = 38;
  115. public const int MaxPathLength = 1024;
  116. /*
  117. INSTALLSTATE MsiGetComponentPath(
  118. LPCTSTR szProduct,
  119. LPCTSTR szComponent,
  120. LPTSTR lpPathBuf,
  121. DWORD* pcchBuf
  122. );
  123. */
  124. [DllImport("msi.dll", CharSet = CharSet.Auto)]
  125. static extern InstallState MsiGetComponentPath(string productCode, string componentCode, StringBuilder componentPath, ref int componentPathBufferSize);
  126. public static string ParseShortcut(string file)
  127. {
  128. StringBuilder product = new StringBuilder(MaxGuidLength + 1);
  129. StringBuilder feature = new StringBuilder(MaxFeatureLength + 1);
  130. StringBuilder component = new StringBuilder(MaxGuidLength + 1);
  131. MsiGetShortcutTarget(file, product, feature, component);
  132. int pathLength = MaxPathLength;
  133. StringBuilder path = new StringBuilder(pathLength);
  134. InstallState installState = MsiGetComponentPath(product.ToString(), component.ToString(), path, ref pathLength);
  135. if (installState == InstallState.Local)
  136. {
  137. return path.ToString();
  138. }
  139. else
  140. {
  141. return null;
  142. }
  143. }
  144. public static string MakeRelativePath(string fromPath, string toPath)
  145. {
  146. string relativePath = null;
  147. try
  148. {
  149. if (string.IsNullOrEmpty(toPath) || string.IsNullOrEmpty(fromPath)) return null;
  150. Uri file = new Uri(@toPath);
  151. // Must end in a slash to indicate folder
  152. Uri folder = new Uri(@fromPath);
  153. relativePath =
  154. Uri.UnescapeDataString(
  155. folder.MakeRelativeUri(file)
  156. .ToString()
  157. .Replace('/', Path.DirectorySeparatorChar)
  158. );
  159. }
  160. catch (Exception ex)
  161. {
  162. LogUtil.WriteErrorLog(ex, "建立相对路径出错:fromPath:" + fromPath + ",toPath:" + toPath);
  163. }
  164. return relativePath;
  165. }
  166. public static FileInfo GetFileByNameWithDir(string name, string dir)
  167. {
  168. DirectoryInfo d = new DirectoryInfo(dir);
  169. FileInfo[] files = d.GetFiles();//文件
  170. foreach (FileInfo fi in files)
  171. {
  172. if (fi.Name.Equals(name))
  173. {
  174. return fi;
  175. }
  176. }
  177. DirectoryInfo[] directs = d.GetDirectories();
  178. foreach (DirectoryInfo direct in directs)
  179. {
  180. return GetFileByNameWithDir(name, direct.FullName);
  181. }
  182. return null;
  183. }
  184. }
  185. }