FileUtil.cs 6.7 KB

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