此文记录的是打开文件和文件夹工具类。
/*** 打开文件和文件夹工具类 Austin Liu 刘恒辉 Project Manager and Software Designer E-Mail: lzhdim@163.com Blog: http://lzhdim.cnblogs.com Date: 2024-01-15 15:18:00 ***/ namespace Lzhdim.LPF.Utility { using System; using System.Diagnostics; /// <summary> /// 打开文件和文件夹工具类 /// </summary> public static class OpenFolderAndFileUtil { /// <summary> /// 打开文件 /// </summary> /// <param name="filePathAndName">文件的路径和名称(比如:C:\Users\Administrator\test.txt)</param> /// <param name="isWaitFileClose">是否等待文件关闭(true:表示等待)</param> public static void OpenFile(string filePathAndName, bool isWaitFileClose = true) { Process process = new Process(); ProcessStartInfo psi = new ProcessStartInfo(filePathAndName); process.StartInfo = psi; process.StartInfo.UseShellExecute = true; try { process.Start(); //等待打开的程序关闭 if (isWaitFileClose) { process.WaitForExit(); } } catch (Exception ex) { throw ex; } finally { process?.Close(); } } /// <summary> /// 打开目录 /// </summary> /// <param name="folderPath">目录路径(比如:C:\Users\Administrator\)</param> public static void OpenFolder(string folderPath) { if (string.IsNullOrEmpty(folderPath)) return; Process process = new Process(); ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe"); psi.Arguments = folderPath; process.StartInfo = psi; try { process.Start(); } catch (Exception ex) { throw ex; } finally { process?.Close(); } } /// <summary> /// 打开目录且选中文件 /// </summary> /// <param name="filePathAndName">文件的路径和名称(比如:C:\Users\Administrator\test.txt)</param> public static void OpenFolderAndSelectedFile(string filePathAndName) { if (string.IsNullOrEmpty(filePathAndName)) return; Process process = new Process(); ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe"); psi.Arguments = "/e,/select," + filePathAndName; process.StartInfo = psi; //process.StartInfo.UseShellExecute = true; try { process.Start(); } catch (Exception ex) { throw ex; } finally { process?.Close(); } } } }
Austin Liu 刘恒辉
Project Manager and Software Designer E-Mail:lzhdim@163.com Blog:https://lzhdim.cnblogs.com 欢迎收藏和转载此博客中的博文,但是请注明出处,给笔者一个与大家交流的空间。谢谢大家。 |