123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- namespace Uestc.Auto6.Dso.U2
- {
- /// <summary>
- /// 资源管理器扩展类
- /// </summary>
- public static class ExplorerExtension
- {
- /// <summary>
- /// 打开路径并定位文件
- /// </summary>
- /// <param name="filePath">文件绝对路径</param>
- 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);
- }
- }
- }
- }
- /// <summary>
- /// 打开文件夹
- /// </summary>
- /// <param name="Path"></param>
- 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);
- }
- }
- }
- }
- /// <summary>
- /// 获取最近保存的文件
- /// </summary>
- /// <param name="Dir"></param>
- /// <param name="Postfix"></param>
- /// <returns></returns>
- public static String GetLatestFile(String Dir, String Postfix = ".set")
- {
- var list = new List<FileTimeInfo>();
- 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; }
- }
- }
|