浏览代码

完善打开文件并定位Explorer的API

yuzhengyang 7 年之前
父节点
当前提交
ca2f2a5d5a
共有 1 个文件被更改,包括 26 次插入15 次删除
  1. 26 15
      Azylee.Utils/Azylee.Core/WindowsUtils/APIUtils/ExplorerAPI.cs

+ 26 - 15
Azylee.Utils/Azylee.Core/WindowsUtils/APIUtils/ExplorerAPI.cs

@@ -35,7 +35,14 @@ namespace Azylee.Core.WindowsUtils.APIUtils
             catch { }
             return false;
         }
-
+        /// <summary>
+        /// 打开并定位到文件
+        /// </summary>
+        /// <param name="filePath"></param>
+        public static void OpenFile(string filePath)
+        {
+            ExplorerFile(filePath);
+        }
         /// <summary>
         /// 打开路径并定位文件...
         /// 对于@"h:\Bleacher Report - Hardaway with the safe call ??.mp4"
@@ -53,26 +60,30 @@ namespace Azylee.Core.WindowsUtils.APIUtils
 
         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
+            try
             {
-                IntPtr pidlList = ILCreateFromPathW(filePath);
-                if (pidlList != IntPtr.Zero)
+                if (!File.Exists(filePath) && !Directory.Exists(filePath))
+                    return;
+
+                if (Directory.Exists(filePath))
+                    Process.Start(@"explorer.exe", "/select,\"" + filePath + "\"");
+                else
                 {
-                    try
+                    IntPtr pidlList = ILCreateFromPathW(filePath);
+                    if (pidlList != IntPtr.Zero)
                     {
-                        Marshal.ThrowExceptionForHR(SHOpenFolderAndSelectItems(pidlList, 0, IntPtr.Zero, 0));
-                    }
-                    finally
-                    {
-                        ILFree(pidlList);
+                        try
+                        {
+                            Marshal.ThrowExceptionForHR(SHOpenFolderAndSelectItems(pidlList, 0, IntPtr.Zero, 0));
+                        }
+                        finally
+                        {
+                            ILFree(pidlList);
+                        }
                     }
                 }
             }
+            catch { }
         }
     }
 }