.net c# winform打开指定目录,选中目录文件,打开文件

版权:原创 更新时间:1年以上
[该文章底部包含文件资源,可根据自己情况,决定是否下载资源使用,时间>金钱,如有需要,立即查看资源]

.net c# 打开目录的方法很多

1. 通用方法,可打开目录和文件.但无法选中目录中的文件.

        /// <summary>
        /// 打开目录或者文件,简洁写法
        /// </summary>
        /// <param name="filepath"></param>
        public static void OpenFileTo(string filepath)
        {
            System.Diagnostics.Process.Start("explorer.exe", filepath);
        }

2.打开指定的目录.

        /// <summary>
        /// 打开目录
        /// </summary>
        /// <param name="path">目录路径</param>
        public static void OpenFolder(string path)
        {
            if (string.IsNullOrEmpty(path)) return;
            Process process = new Process();
            ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe");
            psi.Arguments = path;
            process.StartInfo = psi;
            try
            {
                process.Start();
            }
            finally { process?.Close(); }
        }

3.打开指定的目录并选中指定的文件

        /// <summary>
        /// 打开目录且选中文件
        /// </summary>
        /// <param name="filepath">文件的路径</param>
        public static void OpenFolderChkFile(string filepath)
        {
            if (string.IsNullOrEmpty(filepath)) return;
            Process process = new Process();
            ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe");
            psi.Arguments = "/e,/select," + filepath;
            process.StartInfo = psi;
            try
            {
                process.Start();
            }
            finally { process?.Close(); }
        }

4.打开指定的文件

        /// <summary>
        /// 打开文件
         /// </summary>
        /// <param name="filepath">文件的路径</param>
        public static void OpenFile(string filepath)
        {
            Process process = new Process();
            ProcessStartInfo psi = new ProcessStartInfo(filepath);
            process.StartInfo = psi;
            process.StartInfo.UseShellExecute = true;
            try
            {
                process.Start();
            }
            finally { process?.Close(); }
        }

5.针对部分有特殊符号的,如果需要打开目录,并选中文件

        [DllImport("shell32.dll", ExactSpelling = true)]
        private static extern void ILFree(IntPtr pidlList);

        [DllImport("shell32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern IntPtr ILCreateFromPathW(string pszPath);

        [DllImport("shell32.dll", ExactSpelling = true)]
        private static extern int SHOpenFolderAndSelectItems(IntPtr pidlList, uint cild, IntPtr children, uint dwFlags);

        /// <summary>
        /// 打开路径并定位文件...对于@"c:\aa\A Report - Call ??.mav"这样的,explorer.exe /select,d:xxx不认,用API调整下
        /// 打开文件夹并定位到指定的文件选中,方法加强版,主要是应对路径中含有特殊字符的情况
        /// </summary>
        /// <param name="filepath"></param>
        public static void OpenFileStrong(string filepath)
        {
            if (!File.Exists(filepath) && !Directory.Exists(filepath))
                return;

            if (Directory.Exists(filepath)) Process.Start(@"explorer.exe", "/select,\"" + filepath + "\"");
            else
            {
                IntPtr pidlList = ILCreateFromPathW(filepath);
                if (pidlList != IntPtr.Zero)
                {
                    try
                    {
                        Marshal.ThrowExceptionForHR(SHOpenFolderAndSelectItems(pidlList, 0, IntPtr.Zero, 0));
                    }
                    finally
                    {
                        ILFree(pidlList);
                    }
                }
            }
        }

部分演示效果图如下:


附件:下载该文件资源,减少时间成本(增值服务)
.net c# winform打开指定目录
不商用,只限学习使用,使用后请删除
上传者拥有该资源完整版权,下载既表明已授权您可以进行报备商用,无报备或者使用后报备,视为侵权,报备后使用,为合法有效使用,报备方式,下载后,在下载列表,点击报备,填写使用场景即可
文件部分展示图(单击放大)
.net c# winform打开指定目录.net c# winform打开指定目录.net c# winform打开指定目录
留言
该资源可下载
File Source
.rar
182.38 KB
.net c# winform打开指定目录.net c# winform打开指定目录.net c# winform打开指定目录
最新结算
HTML5 3D效果网页视频背景代码
类型: .rar 金额: CNY 2.31¥ 状态: 待结算 详细>
软件概要设计说明书Word下载
类型: .docx 金额: CNY 0.38¥ 状态: 待结算 详细>
软件概要设计说明书Word下载
类型: .docx 金额: CNY 3.02¥ 状态: 待结算 详细>
.net c#获取声卡信息,声卡数量
类型: .rar 金额: CNY 30.96¥ 状态: 待结算 详细>
.net c#获取声卡信息,声卡数量
类型: .rar 金额: CNY 3.87¥ 状态: 待结算 详细>
.net c#获取电脑的安装软件列表信息,包含名称和版本号
类型: .rar 金额: CNY 30.96¥ 状态: 待结算 详细>
.net c#获取电脑的安装软件列表信息,包含名称和版本号
类型: .rar 金额: CNY 3.87¥ 状态: 待结算 详细>
.net c#获取电脑显示适配器信息
类型: .rar 金额: CNY 30.96¥ 状态: 待结算 详细>
.net c#获取电脑的安装软件列表信息,包含名称和版本号
类型: .rar 金额: CNY 3.87¥ 状态: 待结算 详细>
.net c#获取电脑的安装软件列表信息,包含名称和版本号
类型: .rar 金额: CNY 30.96¥ 状态: 待结算 详细>
各执行环节公正透明,帮助企业完成从赛制策划、活动推广、评委评选到版权转让等系列工作,专业、高效、值得信赖。平均每场赛事可征集到数百至数千组源码作品
合作伙伴
联系我们
  • 邮箱:raozetian@hotmail.com
Copyright 2023-2024 eeigg.com·皖ICP备2024038726号-1
打赏文章