管理

打开文件和文件夹工具类 - C#小函数类推荐

Posted on 2024-10-19 09:30  lzhdim  阅读(9723)  评论(0编辑  收藏  举报

       此文记录的是打开文件和文件夹工具类。

/***

    打开文件和文件夹工具类

    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();
            }
        }
    }
}

 

Copyright © 2000-2022 Lzhdim Technology Software All Rights Reserved