C# - Check if File is Text Based

后端 未结 6 789
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 09:19

How can I test whether a file that I\'m opening in C# using FileStream is a \"text type\" file? I would like my program to open any file that is text based, for example, .t

6条回答
  •  臣服心动
    2020-11-29 09:56

    public bool IsTextFile(string FilePath)
      using (StreamReader reader = new StreamReader(FilePath))
      {
           int Character;
           while ((Character = reader.Read()) != -1)
           {
               if ((Character > 0 && Character < 8) || (Character > 13 && Character < 26))
               {
                        return false; 
               }
           }
      }
      return true;
    }
    

提交回复
热议问题