检查文件是否是txt

        /// <summary>
        /// Checks the file is textfile or not.
        /// </summary>
        /// <param name="fileName"> Name of the file. </param>
        /// <returns></returns>
        public static bool CheckIsTextFile(string fileName)
        {
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            bool isTextFile = true;
            try
            {
                int i = 0;
                int length = (int)fs.Length;
                byte data;
                while (i < length && isTextFile)
                {
                    data = (byte)fs.ReadByte();
                    isTextFile = (data != 0);
                    i++;
                }
                return isTextFile;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }

 这个是问题的,无法跳过html,csv.

posted @ 2022-09-06 09:15  vba是最好的语言  阅读(134)  评论(0编辑  收藏  举报