Windows Phone 8 choose text file C#

牧云@^-^@ 提交于 2019-12-11 04:27:38

问题


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?


回答1:


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;



回答2:


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



来源:https://stackoverflow.com/questions/27166253/windows-phone-8-choose-text-file-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!