How to read a text file with a different encoding than UFT8 or UTF16 in WinRT?

If I read a text file using FileIO.ReadTextAsync, ReadLinesAsync or DataReader, I can only specify a member of the UnicodeEncoding enumeration for encoding. This includes, for some reason, only Utf8, Utf16BE and Utf16LE. How can I read a text file with a different encoding (for example, Windows-1252 or even regular Unicode (with 2 bytes for all characters)) ???

This can be important if Windows Store Apps is exchanging text files with Desktop applications or reading text files from the Internet.

+4
source share
2 answers

Hans comment really answered my question. Sample for Windows-1252:

string filePath = ... StorageFile file = await StorageFile.GetFileFromPathAsync(filePath); IBuffer buffer = await FileIO.ReadBufferAsync(file); byte[] fileData = buffer.ToArray(); Encoding encoding = Encoding.GetEncoding("Windows-1252"); string text = encoding.GetString(fileData, 0, fileData.Length); 
+4
source

@ JürgenBayer buffer.ToArray() not available to me.

So instead of writing:

 string text = await FileIO.ReadTextAsync(file); 

I wrote:

 IBuffer buffer = await FileIO.ReadBufferAsync(file); byte[] fileData; CryptographicBuffer.CopyToByteArray(buffer, out fileData); Encoding encoding = Encoding.GetEncoding("Windows-1252"); string text = encoding.GetString(fileData, 0, fileData.Length); 
0
source

Source: https://habr.com/ru/post/1440301/


All Articles