Skip to content

Read All Text from Textfile with Encoding in Windows RT

January 15, 2013

A simple extension for reading all text from a text file in WinRT with a specific encoding, made as an extension to StorageFile.

public static class StorageFileExtensions 
{ 
    async public static Task<string> 
        ReadAllTextAsync(this StorageFile storageFile) 
    { 
        var buffer = await FileIO.
            ReadBufferAsync(storageFile); 
        var fileData = buffer.ToArray(); 
        var encoding = Encoding.GetEncoding
            ("Windows-1252"); 
        var text = encoding.GetString
            (fileData, 0, fileData.Length); 
        return text; 
    } 
}

From → C#, Metro UI, Windows 8 RT

Leave a Comment

Leave a comment