C#打开指定目录 文件

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C#打开指定目录 文件

牛奶咖啡13   2022-09-08 我要评论

一、实现内容

1.1实现的功能

想要实现:

①打开指定的目录;

②打开指定的目录且选中指定文件;

③打开指定文件

1.2实现的效果

二、实现操作

        /// <summary>
        /// 打开目录
        /// </summary>
        /// <param name="folderPath">目录路径(比如:C:\Users\Administrator\)</param>
        private 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>
        private 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();
 
            }
        }
 
        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="filePathAndName">文件的路径和名称(比如:C:\Users\Administrator\test.txt)</param>
        /// <param name="isWaitFileClose">是否等待文件关闭(true:表示等待)</param>
        private 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();
               
            }
        }

三、Windows 资源管理器参数说明

Windows资源管理器参数的说明

序号参数命令说明
1Explorer /n此命令使用默认设置打开一个资源管理器窗口。显示的内容通常是安装 Windows 的驱动器的根目录
2Explorer /e此命令使用默认视图启动 Windows 资源管理器
3Explorer /e,C:\Windows此命令使用默认视图启动 Windows 资源管理器,并把焦点定位在 C:\Windows路径上
4Explorer /root, C:\Windows\Cursors此命令启动 Windows 资源管理器后焦点定位在 C:\Windows\Cursors folder路径上。此示例使用 C:\Windows\Cursors 作为 Windows 资源管理器的“根”目录
5Explorer /select, C:\Windows\Cursors\banana.ani此命令启动 Windows 资源管理器后选定“C:\Windows\Cursors\banana.ani”文件。
6Explorer /root, \\server\share, select, Program.exe此命令启动 Windows 资源管理器时以远程共享作为“根”文件夹,而且 Program.exe 文件将被选中

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们