C# 打开目录并选中文件的相关问题

C# 打开文件夹并选中目录,本来很简单的一个要求,按照网上说的怎么也实现不了。

总是打开“我的文档”


后来多次尝试才知道是路径的问题,如果不是标准的路径是无法打开的。

比如路径中有 "\\\\" 这样的路径分隔符、估计是底层的C++参数输入要求太严格了。

我改成下面这样的处理。现在没发现其他的问题。


string LocalPath = Common.ChangedToStandardPath(OriginalLocalPath,'\\');
//LocalPath 为文件路径

 System.Diagnostics.Process.Start("Explorer", "/select," + LocalPath);


将不规则的路径转换为标准的路径
/// <summary>
        /// 路径转换为标准路径
        /// </summary>
        /// <param name="strPath">路径</param>
        /// <param name="SplitChar">分割字符串</param>
        /// <returns></returns>
        public static string ChangedToStandardPath(string strPath,char SplitChar)
        {
            string strStandrtPath = "";
            string[] Arry = strPath.Split(SplitChar);
            foreach (string SplitString in Arry)
            {
                if (string.IsNullOrEmpty(SplitString) == false && SplitString.Equals(SplitChar) == false)
                {
                    strStandrtPath += (SplitString + SplitChar);
                }
            }
            strStandrtPath = strStandrtPath.Substring(0, strStandrtPath.Length -1);
            return strStandrtPath;
        }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值