判断文件路径、url是否可用

来源:互联网 发布:花椒网络直播 编辑:程序博客网 时间:2024/06/07 11:13

url的正则验证很值得参考

         /// <summary>        /// 判断绝对路径是否合法。        /// </summary>        /// <param name="path"></param>        /// <returns></returns>        public static bool IsAbosolutePath(string path)        {            string pattern = @"^\s*([a-zA-Z]:\\|\\\\)([^\^\/:*?""<>|]+\\)*([^\^\/:*?""<>|]+)$";              Regex regex1 = new Regex(pattern);            return regex1.IsMatch(path);          }        /// <summary>        /// 判断绝对路径是否有效        /// </summary>        /// <param name="path"></param>        /// <returns></returns>        public static bool AbosolutePathIsValid(string path)        {            if (IsAbosolutePath(path))            {                if (File.Exists(path))                {                    return true;                }            }            return false;        }        /// <summary>        /// 判断url是否合法        /// </summary>        /// <param name="url"></param>        /// <returns></returns>        public static bool IsUrl(string url)        {            var pattern = "^((https|http|ftp|rtsp|mms)?://)?"                          + "("                          + "([0-9]{1,3}\\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184                             + "|"                          + "([0-9a-zA-Z_!~*'()-]+\\.)*" // 域名- www.                              + "([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\\." // 二级域名                               + "[a-zA-Z]{2,6}" // first level domain- .com or .museum                             + ")"                          + "(:[0-9]{1,4})?" // 端口- :80                           + "("                          + "(/?)"                          + "|"                          + "(/[0-9a-zA-Z_!~*'().;?:@&=+$,%#-]+)"                          + "+/?"                          + ")$";            Regex regex1 = new Regex(pattern);            return regex1.IsMatch(url);        }        /// <summary>        /// 判断url是否有效        /// </summary>        /// <param name="url"></param>        /// <returns></returns>        public static bool UrlIsValid(string url)        {            if (IsUrl(url))            {                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);                myRequest.Method = "HEAD";               //设置提交方式可以为"get","head"等                myRequest.Timeout = 10000;              //设置网页响应时间长度                myRequest.AllowAutoRedirect = false;//是否允许自动重定向                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();                if (myResponse.StatusCode == HttpStatusCode.OK) ;//返回响应的状态                {                    return true;                }            }            return false;        }


1 0
原创粉丝点击