Windows Phone 8 choose text file C#

99 views Asked by At

i have a question. If there is a possibility at windows phone 8 at visual studio to create button event to read text file? i know about streamReader and if i declare wchich exacly file i want to read, but if i want to choose from list of files wchich i want to display. i did research on the Internet but i didint find an answer. I know i can use isolatedStorage to read music, video, image but not text files, on the app i created few files with text in it and i want users to have posibility to display one from this file, whichever they want to see. So, can you tell me how to do this?

2

There are 2 answers

0
Chubosaurus Software On BEST ANSWER

You can use IsolatedStorage to read any file type you wish. You must of been using something like a Launcher that filters out the file type based on the Chooser.

You can open a file like this:

private async Task<string> ReadTextFile(string file_name)
{
    // return buffer
    string file_content = "";

    // Get the local folder
    StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;

    if (local != null)
    {
        // Get the file
        StorageFile file;
        try
        {
            file = await local.GetFileAsync(file_name);
        }
        catch (Exception ex)
        {
            // no file, return empty
            return file_content;
        }

        // Get the stream
        System.IO.Stream file_stream = await file.OpenStreamForReadAsync();

        // Read the data
        using (StreamReader streamReader = new StreamReader(file_stream))
        {
           file_content = streamReader.ReadToEnd();   // read the full text file 
           streamReader.Close();
        }

        // Close the stream
        file_stream.Close();
    }

    // return
    return file_content;
}

If you want to get the PackageLocation (files that you added into the project like assets and images) then replace the LocalFolder with

Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
0
user3074358 On

With Windows Phone 8.1, File Pickers are allowed, consisting the same functionality you are expecting, so probably you might want to upgrade your app to WP8.1.

Here's more info on this API : Working with File Pickers