Exemplo n.º 1
0
        /// <summary>
        /// 校验文件路径是否合法,示例:
        /// <para>C:\1.txt</para>
        /// <para>C:\folder\1.txt</para>
        /// <para>C:\folder\1txt</para>
        /// <para>IOExtNameException异常</para>
        /// </summary>
        /// <param name="path">需校验的完整路径</param>
        /// <param name="ext">扩展名(不含.)
        /// <para>若ext="",则不校验扩展名</para>
        /// <para>若ext=*,则校验任意扩展名</para>
        /// <para>否则,校验指定扩展名</para>
        /// </param>
        /// <returns></returns>
        public static bool CheckFilePath(string path, string ext = "")
        {
            // 解析:[盘符:] + 1或多段[\合法文件夹或文件名]
            string pattern;

            switch (ext)
            {
            case "":
                pattern = RegexExt.FilePath;
                break;

            case "*":
                pattern = RegexExt.FilePath2;
                break;

            default:
                if (!CheckExt(ext))
                {
                    throw new IOExtNameException(ext);
                }
                pattern = $@"^{RegexExt.AvailDisk}:({RegexExt.AvailSplit}[^{RegexExt.NoAvailChar}]+)+(?<=\.{ext.ToRegex()})$";
                break;
            }

            return(RegexExt.IsMatchNoCase(path, pattern));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 校验文件名称是否合法
        /// <para>IOExtNameException异常</para>
        /// </summary>
        /// <param name="name">文件完整名称</param>
        /// <param name="ext">扩展名(不含.)</param>
        /// <returns></returns>
        public static bool CheckFileName(string name, string ext = "")
        {
            string pattern;

            switch (ext)
            {
            case "":
                pattern = RegexExt.FileName;
                break;

            case "*":
                pattern = RegexExt.FileName2;
                break;

            default:
                if (!CheckExt(ext))
                {
                    throw new IOExtNameException(ext);
                }
                pattern = $@"^[^{RegexExt.NoAvailChar}]+\.{ext.ToRegex()}$";
                break;
            }

            return(RegexExt.IsMatchNoCase(name, pattern));
        }