using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices; namespace Uestc.Auto6.Dso.U2 { /// /// 资源管理器扩展类 /// public static class ExplorerExtension { /// /// 打开路径并定位文件 /// /// 文件绝对路径 public static void ExplorerFile(String filePath) { if (!File.Exists(filePath) && !Directory.Exists(filePath)) { return; } if (Directory.Exists(filePath)) { Process.Start(@"explorer.exe", "/select,\"" + filePath + "\""); } else { IntPtr pidlList = NativeMethods.ILCreateFromPathW(filePath); if (pidlList != IntPtr.Zero) { try { Marshal.ThrowExceptionForHR(NativeMethods.SHOpenFolderAndSelectItems(pidlList, 0, IntPtr.Zero, 0)); } finally { NativeMethods.ILFree(pidlList); } } } } /// /// 打开文件夹 /// /// public static void ExplorerDic(String Path) { if (!File.Exists(Path) && !Directory.Exists(Path)) { return; } if (Directory.Exists(Path)) { Process.Start(@"explorer.exe", Path); } else { IntPtr pidlList = NativeMethods.ILCreateFromPathW(Path); if (pidlList != IntPtr.Zero) { try { Marshal.ThrowExceptionForHR(NativeMethods.SHOpenFolderAndSelectItems(pidlList, 0, IntPtr.Zero, 0)); } finally { NativeMethods.ILFree(pidlList); } } } } /// /// 获取最近保存的文件 /// /// /// /// public static String GetLatestFile(String Dir, String Postfix = ".set") { var list = new List(); var dicInfo = new DirectoryInfo(Dir); foreach (var file in dicInfo.GetFiles()) { if (String.IsNullOrEmpty(Postfix)) { list.Add(new FileTimeInfo() { FileName = file.FullName, LastWriteTime = file.LastWriteTime, }); } else if (file.Extension.ToUpper() == Postfix.ToUpper()) { list.Add(new FileTimeInfo() { FileName = file.FullName, LastWriteTime = file.LastWriteTime, }); } } var f = from x in list orderby x.LastWriteTime select x; if (f.Count() == 0) { return String.Empty; } return f.LastOrDefault().FileName; } } internal class FileTimeInfo { public String FileName { get; set; } public DateTime LastWriteTime { get; set; } } }