Browse Source

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

yuzhengyang 7 years ago
parent
commit
ca2f2a5d5a
1 changed files with 26 additions and 15 deletions
  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 { }
             catch { }
             return false;
             return false;
         }
         }
-
+        /// <summary>
+        /// 打开并定位到文件
+        /// </summary>
+        /// <param name="filePath"></param>
+        public static void OpenFile(string filePath)
+        {
+            ExplorerFile(filePath);
+        }
         /// <summary>
         /// <summary>
         /// 打开路径并定位文件...
         /// 打开路径并定位文件...
         /// 对于@"h:\Bleacher Report - Hardaway with the safe call ??.mp4"
         /// 对于@"h:\Bleacher Report - Hardaway with the safe call ??.mp4"
@@ -53,26 +60,30 @@ namespace Azylee.Core.WindowsUtils.APIUtils
 
 
         public static void ExplorerFile(string filePath)
         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 { }
         }
         }
     }
     }
 }
 }