Milan Aggarwal
Milan Aggarwal

Reputation: 5104

How to read a file in windows 8 metro App

I want to ship a text file along with my App and read it while executing my application. How can I read that particular file? I have set the file to content and copy if newer.

Upvotes: 1

Views: 1821

Answers (2)

Milan Aggarwal
Milan Aggarwal

Reputation: 5104

private async void ProjectFile()
{
    var _Path = @"Metro.Helpers.Tests\MyFolder\MyFolder.txt";
    var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;

    var _File = await _Folder.GetFileAsync(_Path);
    var _ReadThis = await Windows.Storage.FileIO.ReadTextAsync(_File);
}

Code to read project file which works for me. Hope it helps others too :)

Upvotes: 4

ZombieSheep
ZombieSheep

Reputation: 29963

The answer to your question is heavily dependant on how you want to read the file, and what it contains.

You can find a few examples of file access in the File Access Sample app on MSDN, from which one example is:

if (file != null) 
{ 
    using (IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read)) 
    { 
        using (DataReader dataReader = new DataReader(readStream)) 
        { 
            UInt64 size = readStream.Size; 
            if (size <= UInt32.MaxValue) 
            { 
                UInt32 numBytesLoaded = await dataReader.LoadAsync((UInt32)size); 
                string fileContent = dataReader.ReadString(numBytesLoaded); 
                OutputTextBlock.Text = "The following text was read from '" + file.Name + "' using a stream:" + Environment.NewLine + Environment.NewLine + fileContent; 
            } 
            else 
            { 
                OutputTextBlock.Text = "File " + file.Name + " is too big for LoadAsync to load in a single chunk. Files larger than 4GB need to be broken into multiple chunks to be loaded by LoadAsync."; 
            } 
        } 
    } 
} 

Upvotes: 2

Related Questions