   [.net] How do I open files in Binary Mode?
  In C#, how do I open a file in binary mode? I've run into a problem where I turn the text of a file into a string, and some of the chars in that string have value > 255 (damn unicode). So I need to open it in binary mode now to ensure that I get the data from the file byte by byte.How do I open a file in binary mode?Up til now I've been using FileInfo info = new FileInfo(@"myFilename");StreamReader stream = info.OpenText();(I just confirmed that this is in fact UTF8 mode and not binary)Thanks for the help!   
  Ok, so here it goesIO.FileStream fs = new IO.FileStream(FilePath, IO.FileMode.Open);IO.StreamReader sr = new IO.StreamReader(fs);//At this point you can read the file usingstring FileContent;FileContent = sr.ReadToEnd//Or read Line by Line usingFileContent = sr.ReadLine//Or you can read to content to an array of bytesr.Read(mybytearray, 0,fs.Length());I hope thats what you were looking for   ;  You can use the BinaryReader class to read binary data from files (streams).using (BinaryReader br = new BinaryReader(new FileStream(@"myFilename", FileMode.Open))) {    // Read!}I'm not too hot on the TextReader class, but there should be a way to set the encoding so it doesn't decide to treat the file as having Unicode characters in it. If you just want the binary data, though, use a BinaryReader.   ;  There's no binary mode in .NET. A FileStream just gives you the raw bytes without any transformation. If you want to read text, you need to put something like a StreamReader around it.   ;  Heres a snippit of code from my tileeditor in c#.  I wrote all the strings out as chars to enable the file to be used with c++. Before the string i wrote out the length of the string and read that amount of chars.  Hope it helps.{FileStream file;BinaryReader br;try{file = new FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);}catch (Exception e){MessageBox.Show(e.Message);return false;}br = new BinaryReader(file);int layers;layers = br.ReadInt32();this.m_map.MapLayers = new ArrayList();this.m_map.NumLayers = layers;for (int layerCount = 0; layerCount < layers; layerCount++){int rows = br.ReadInt32();int cols = br.ReadInt32();int len = br.ReadInt32();LayerInfo layer = new LayerInfo();layer.NumRows = rows;layer.NumCols = cols;layer.TileMap = new TileInfo[rows, cols];char [] temp = br.ReadChars(len);layer.LayerTileSet = new string(temp);;  If you use StreamReader sr = new StreamReader("filename.txt", Encoding.Default) I think that will get you a string with chars between 32 and 255. If it's not that, try Encoding.None; I forget which one works.If you want to be able to cope with all values (i.e. it is a binary file not a non-Unicode text file) you want to use BinaryReader as suggested above.   ;  Use StreamReader as Arild Fines mentioned, but if you have specific file format use RizMan's code. Actually both variation support unicode strings the only thing is the implementation.   ;  Now, if I use the BinaryReader class, how can I read 1 line (up to a newline, or a unicode newline) when I DO open a text file?   ;Quote:Original post by NexusOneNow, if I use the BinaryReader class, how can I read 1 line (up to a newline, or a unicode newline) when I DO open a text file?You wouldn't use a BinaryReader to open a text file, you'd use a TextReader (with the correct encoding).   