在 C# UWP 中加载 .txt 文件
Loading .txt file in C# UWP
我正在尝试为我的 UPW 应用程序打开一个 .txt 文件。
string text;
private async void OpenFile(string fileName)
{
StorageFolder localFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("Beginner");
try
{
StorageFile file = await localFolder.GetFileAsync(fileName + ".txt");
text = await FileIO.ReadTextAsync(file);
PrintMessage(text);
}
catch (Exception)
{
PrintMessage("Failed to load file");
}
}
public async void PrintMessage(string message)
{
//Writes message
MessageDialog msg = new MessageDialog(message);
await msg.ShowAsync();
}
public Main()
{
OpenFile("WordList");
PrintMessage(text);
}
当我在 ReadTextAsync
之后有 PrintMessage(text);
时,代码 运行 没问题。当我删除它时,程序大部分时间都会冻结。当我在调试器中 运行 它时,它大部分都完成了,但有时它会在 StorageFile file = await localFolder.GetFileAsync(fileName + ".txt");
行冻结。我相信异步魔术存在一些问题。
此外,Main()
中的 text
变量几乎总是 null,即使它应该包含文件的文本。
我所需要的只是一个能够可靠地打开 .txt 文件和 return(return return 或像这样进入全局)内容的函数该文件的。我尝试使用 Task<>
但我也无法使其工作...
编辑:
我试过了
string text { get; set; }
public async void OpenFileAssist(string fileName)
{
await OpenFile(fileName);
}
public async Task OpenFile(string fileName)
{
StorageFolder localFolder = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFolderAsync("Beginner");
try
{
StorageFile file = await localFolder.GetFileAsync(fileName + ".txt");
text = await FileIO.ReadTextAsync(file);
}
catch (Exception)
{
PrintMessage("File does not exist");
}
}
但它仍然不起作用...当我从 Main() 调用 OpenFileAssist() 时,文本变量仍然为空。 :( 不过,如果我在调试器中打开它并逐步执行它,它就会工作。
我通过
找到了解决方案
private async void Page_Loaded(object sender, RoutedEventArgs e)
参见 this page,尤其是使用 TaskScheduler.FromCurrentSynchronizationContext() 的示例。对我帮助很大。
我正在尝试为我的 UPW 应用程序打开一个 .txt 文件。
string text;
private async void OpenFile(string fileName)
{
StorageFolder localFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("Beginner");
try
{
StorageFile file = await localFolder.GetFileAsync(fileName + ".txt");
text = await FileIO.ReadTextAsync(file);
PrintMessage(text);
}
catch (Exception)
{
PrintMessage("Failed to load file");
}
}
public async void PrintMessage(string message)
{
//Writes message
MessageDialog msg = new MessageDialog(message);
await msg.ShowAsync();
}
public Main()
{
OpenFile("WordList");
PrintMessage(text);
}
当我在 ReadTextAsync
之后有 PrintMessage(text);
时,代码 运行 没问题。当我删除它时,程序大部分时间都会冻结。当我在调试器中 运行 它时,它大部分都完成了,但有时它会在 StorageFile file = await localFolder.GetFileAsync(fileName + ".txt");
行冻结。我相信异步魔术存在一些问题。
此外,Main()
中的 text
变量几乎总是 null,即使它应该包含文件的文本。
我所需要的只是一个能够可靠地打开 .txt 文件和 return(return return 或像这样进入全局)内容的函数该文件的。我尝试使用 Task<>
但我也无法使其工作...
编辑: 我试过了
string text { get; set; }
public async void OpenFileAssist(string fileName)
{
await OpenFile(fileName);
}
public async Task OpenFile(string fileName)
{
StorageFolder localFolder = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFolderAsync("Beginner");
try
{
StorageFile file = await localFolder.GetFileAsync(fileName + ".txt");
text = await FileIO.ReadTextAsync(file);
}
catch (Exception)
{
PrintMessage("File does not exist");
}
}
但它仍然不起作用...当我从 Main() 调用 OpenFileAssist() 时,文本变量仍然为空。 :( 不过,如果我在调试器中打开它并逐步执行它,它就会工作。
我通过
找到了解决方案private async void Page_Loaded(object sender, RoutedEventArgs e)
参见 this page,尤其是使用 TaskScheduler.FromCurrentSynchronizationContext() 的示例。对我帮助很大。