1

我想随我的应用程序一起发送一个文本文件,并在执行我的应用程序时阅读它。我怎样才能读取那个特定的文件?我已将文件设置为内​​容并复制(如果更新)。

4

2 回答 2

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

读取适合我的项目文件的代码。希望它也可以帮助其他人:)

于 2012-09-21T10:49:29.283 回答
2

您的问题的答案在很大程度上取决于您希望如何读取文件以及它包含的内容。

您可以在 MSDN 上的文件访问示例应用程序中找到一些文件访问示例,其中一个示例是:

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."; 
            } 
        } 
    } 
} 
于 2012-09-21T10:01:54.403 回答