首页 > Windows开发 > 详细

UNC 目录格式检测C#代码

时间:2016-08-08 18:53:22      阅读:246      评论:0      收藏:0      [点我收藏+]
 /// <summary>
        /// if  path is UNC( Universal Naming Convention) path return or return false.
        /// formatter:\\servername\sharename
        /// </summary>
        /// <param name="path">path</param>
        /// <returns></returns>
        public static bool IsUNCPath(string path)
        {
            if (!path.StartsWith(@"\\"))
            {
                return false;
            }
            var serverNameRejectCharacters = @"`~!@#$%^&*()=+_[]{}\|;:.‘,<>/?";
            var serverNameRejectCharactersArr = serverNameRejectCharacters.ToList();
            serverNameRejectCharactersArr.Add(‘\"‘);
            var folderNameRejectCharacters = @"/:*?<>|";
            var folderNameRejectCharactersArr = folderNameRejectCharacters.ToList();
            folderNameRejectCharactersArr.Add(‘\"‘);
            if (path.Length < 5)
            {
                return false;
            }
            else
            {
                // third ‘\‘ index in path
                var third = path.Remove(0, 2).IndexOf(‘\\‘);
                var serverName = path.Substring(2, third - 1);
                for (int i = 0; i < serverName.Length; i++)
                {
                    for (int j = 0; j < serverNameRejectCharactersArr.Count; j++)
                    {
                        if (serverName[i] == serverNameRejectCharactersArr[j])
                        {
                            return false;
                        }
                    }

                }
                var pathes = path.Remove(0, 2 + serverName.Length);
                if (pathes.Contains(@"\\"))
                {
                    return false;
                }
                else
                {
                    for (int i = 0; i < pathes.Length; i++)
                    {
                        for (int j = 0; j < folderNameRejectCharactersArr.Count; j++)
                        {
                            if (pathes[i] == folderNameRejectCharactersArr[j])
                            {
                                return false;
                            }
                        }
                    }
                }

            }
            return true;
        }

  

UNC 目录格式检测C#代码

原文:http://www.cnblogs.com/doublejun/p/5750446.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!