How to use ReadAllText when file encoding unknown

The only way to reliably do this is to look for byte order marks at the start of the text file. (This blob more generally represents the endianness of character encoding used, but also the encoding - e.g. UTF8, UTF16, UTF32). Unfortunately, this method only works for Unicode-based encodings, and nothing before that (for which much less reliable methods must be used).

The StreamReader type supports detecting these marks to determine the encoding - you simply need to pass a flag to the parameter as such:

new System.IO.StreamReader("path", true)

You can then check the value of stremReader.CurrentEncoding to determine the encoding used by the file. Note however that if no byte encoding marks exist, then CurrentEncoding will default to Encoding.Default.

Refer codeproject solution to detect encoding


You have to check file encoding first. try this

System.Text.Encoding enc = null; 
System.IO.FileStream file = new System.IO.FileStream(filePath, 
    FileMode.Open, FileAccess.Read, FileShare.Read); 
if (file.CanSeek) 
{ 
    byte[] bom = new byte[4]; // Get the byte-order mark, if there is one 
    file.Read(bom, 0, 4); 
    if ((bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) || // utf-8 
        (bom[0] == 0xff && bom[1] == 0xfe) || // ucs-2le, ucs-4le, and ucs-16le 
        (bom[0] == 0xfe && bom[1] == 0xff) || // utf-16 and ucs-2 
        (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff)) // ucs-4 
    { 
        enc = System.Text.Encoding.Unicode; 
    } 
    else 
    { 
        enc = System.Text.Encoding.ASCII; 
    } 

    // Now reposition the file cursor back to the start of the file 
    file.Seek(0, System.IO.SeekOrigin.Begin); 
} 
else 
{ 
    // The file cannot be randomly accessed, so you need to decide what to set the default to 
    // based on the data provided. If you're expecting data from a lot of older applications, 
    // default your encoding to Encoding.ASCII. If you're expecting data from a lot of newer 
    // applications, default your encoding to Encoding.Unicode. Also, since binary files are 
    // single byte-based, so you will want to use Encoding.ASCII, even though you'll probably 
    // never need to use the encoding then since the Encoding classes are really meant to get 
    // strings from the byte array that is the file. 

    enc = System.Text.Encoding.ASCII; 
}

Comments

  1. Lombardo

    • 2019/7/29

    The only way to reliably do this is to look for byte order marks at the start of the text file. (This blob more generally represents the 

  2. Brooks

    • 2020/7/1

    c# - How to use ReadAllText when file encoding unknown - Stack Overflow. Im reading a file with ReadAllText String[] values = File.ReadAllText(@"c:\\c\\file.txt").Split(';'); int i = 0; foreach (String s in values) { System.Console.WriteLine(". Stack Overflow.

  3. Scott

    • 2016/4/3

    The file is a .dat file of unknown encoding (but something very old) that easily opens in I've tried switching encoding using Encoding.

  4. Morris

    • 2021/1/30

    Dim readText As String = File.ReadAllText(path) Console.WriteLine(readText) End Sub End Class Remarks. This method opens a file, reads all the text in the file, and returns it as a string. It then closes the file. This method attempts to automatically detect the encoding of a file based on the presence of byte order marks.

  5. Giuliani

    • 2018/7/11

    NET provides encoding classes that encode and decode text by using various encoding systems. For example, the UTF8Encoding class describes the 

  6. David

    • 2015/1/20

    Reading text files with ReadAllText() with encoding in mind. Reading text files with ReadAllText() with encoding in mind.

  7. David

    • 2017/7/27

    I saved the file with the default ANSI encoding. ansi save. I'm going to read names from this text file using a .NET Framework StreamReader 

  8. Harold

    • 2017/10/12

    File.ReadAllText (String) is an inbuilt File class method that is used to open a text file then reads all the text in the file and then closes the file. Syntax: public static string ReadAllText (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the specified file to open for reading.

  9. Quinn

    • 2020/1/12

    File: HttpUtility.cs Project: mahanteshck/antlrcs private static string ReadAllText(string path, Encoding encoding) { using (var reader = new 

  10. Legacy

    • 2017/10/21

    The ReadAllText method of the My.Computer.FileSystem object allows you to read from a text file. The contents of the file are returned as a string. The file encoding can be specified if the contents of the file are in an encoding such as ASCII or UTF-8. If you are reading from a file with extended characters, you need to specify the file encoding.

  11. Musa

    • 2020/11/4

    InternalReadAllText(String path, Encoding encoding, Boolean checkHost) at System.IO.File.ReadAllText(String path) at ConsoleApp.Program.

  12. Smith

    • 2017/6/14

    Last Updated : 09 Mar, 2021. File.ReadAllText (String, Encoding) is an inbuilt File class method that is used to open a text file then reads all the text in the file with the specified encoding and then closes the file. Syntax: public static string ReadAllText (string path, System.Text.Encoding encoding); Parameter: This function accepts a parameter which is illustrated below:

  13. Villa

    • 2015/8/21

    Character Encoding Detection. If encoding_list is omitted, detect_order is used. Lets take this string : teamultimate. Definition and Usage. js to UTF-8-encode 

  14. Prifti

    • 2018/9/22

    If the file looks like this: 1234 4567 9012. Then it will read and display fine. However, if the file looks like this: 1234 5678 9012. It will stop after "5678." The following is the code I have: string FileToOpen = lstMyListHere.SelectedItem.ToString(); txtTextboxToPrintTo.Text = File.ReadAllText(FileToOpen);

  15. Zander

    • 2019/4/4

    so, i'm using next method files void replaceinfile(string file, string oldvalue, string newvalue) { string text = file.readalltext(file); 

  16. Brodie

    • 2017/6/17

    Sometimes we can also use a data step to read in an ASCII data file. When you're working with text files and want transparent encoding and decoding into 

  17. Tru

    • 2018/9/3

    String Use the encoding type for a string. File]::ReadAllText(string path) -Wait [Dynamic Parameter (FileSystem Only)] Wait for content to be appended 

Comments are closed.

Recent Posts