Windows Phone 8 选择文本文件 C# [英] Windows Phone 8 choose text file C#

查看:28
本文介绍了Windows Phone 8 选择文本文件 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有个问题.如果在visual studio的windows phone 8上有可能创建按钮事件来读取文本文件?我知道streamReader,如果我声明我想要阅读的确切文件,但是如果我想从我想要显示的文件列表中进行选择.我在互联网上做了研究,但我没有找到答案.我知道我可以使用 isolatedStorage 来读取音乐、视频、图像但不能读取文本文件,在应用程序上我创建了几个带有文本的文件,我希望用户能够从这个文件中显示一个,无论他们想看哪个.所以,你能告诉我怎么做吗?

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?

推荐答案

您可以使用 IsolatedStorage 来读取您希望的任何文件类型.您一定使用过类似 Launcher 之类的东西,它会根据选择器过滤掉文件类型.

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;
}

<小时>

如果您想获得 PackageLocation(您添加到项目中的文件,如资产和图像),请将 LocalFolder 替换为

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

这篇关于Windows Phone 8 选择文本文件 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆