ExplorerExtension.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. namespace Uestc.Auto6.Dso.U2
  8. {
  9. /// <summary>
  10. /// 资源管理器扩展类
  11. /// </summary>
  12. public static class ExplorerExtension
  13. {
  14. /// <summary>
  15. /// 打开路径并定位文件
  16. /// </summary>
  17. /// <param name="filePath">文件绝对路径</param>
  18. public static void ExplorerFile(String filePath)
  19. {
  20. if (!File.Exists(filePath) && !Directory.Exists(filePath))
  21. {
  22. return;
  23. }
  24. if (Directory.Exists(filePath))
  25. {
  26. Process.Start(@"explorer.exe", "/select,\"" + filePath + "\"");
  27. }
  28. else
  29. {
  30. IntPtr pidlList = NativeMethods.ILCreateFromPathW(filePath);
  31. if (pidlList != IntPtr.Zero)
  32. {
  33. try
  34. {
  35. Marshal.ThrowExceptionForHR(NativeMethods.SHOpenFolderAndSelectItems(pidlList, 0, IntPtr.Zero, 0));
  36. }
  37. finally
  38. {
  39. NativeMethods.ILFree(pidlList);
  40. }
  41. }
  42. }
  43. }
  44. /// <summary>
  45. /// 打开文件夹
  46. /// </summary>
  47. /// <param name="Path"></param>
  48. public static void ExplorerDic(String Path)
  49. {
  50. if (!File.Exists(Path) && !Directory.Exists(Path))
  51. {
  52. return;
  53. }
  54. if (Directory.Exists(Path))
  55. {
  56. Process.Start(@"explorer.exe", Path);
  57. }
  58. else
  59. {
  60. IntPtr pidlList = NativeMethods.ILCreateFromPathW(Path);
  61. if (pidlList != IntPtr.Zero)
  62. {
  63. try
  64. {
  65. Marshal.ThrowExceptionForHR(NativeMethods.SHOpenFolderAndSelectItems(pidlList, 0, IntPtr.Zero, 0));
  66. }
  67. finally
  68. {
  69. NativeMethods.ILFree(pidlList);
  70. }
  71. }
  72. }
  73. }
  74. /// <summary>
  75. /// 获取最近保存的文件
  76. /// </summary>
  77. /// <param name="Dir"></param>
  78. /// <param name="Postfix"></param>
  79. /// <returns></returns>
  80. public static String GetLatestFile(String Dir, String Postfix = ".set")
  81. {
  82. var list = new List<FileTimeInfo>();
  83. var dicInfo = new DirectoryInfo(Dir);
  84. foreach (var file in dicInfo.GetFiles())
  85. {
  86. if (String.IsNullOrEmpty(Postfix))
  87. {
  88. list.Add(new FileTimeInfo()
  89. {
  90. FileName = file.FullName,
  91. LastWriteTime = file.LastWriteTime,
  92. });
  93. }
  94. else if (file.Extension.ToUpper() == Postfix.ToUpper())
  95. {
  96. list.Add(new FileTimeInfo()
  97. {
  98. FileName = file.FullName,
  99. LastWriteTime = file.LastWriteTime,
  100. });
  101. }
  102. }
  103. var f = from x in list
  104. orderby x.LastWriteTime
  105. select x;
  106. if (f.Count() == 0)
  107. {
  108. return String.Empty;
  109. }
  110. return f.LastOrDefault().FileName;
  111. }
  112. }
  113. internal class FileTimeInfo
  114. {
  115. public String FileName { get; set; }
  116. public DateTime LastWriteTime { get; set; }
  117. }
  118. }