几周前,我在此主题中提出了类似的问题Write and Read from a file并且我的问题得到了回答,但是现在我正在尝试读取文件并使用 if 函数,但目前我只能从文本块中读取通过按一个按钮来读取文件并传输到 txtblock,所以我想知道如何能够不使用这种方式进行读取。此刻在这里。
private async Task ReadFile()
{
// Get the local folder.
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
if (local != null)
{
// Get the DataFolder folder.
var dataFolder = await local.GetFolderAsync("level");
// Get the file.
var file = await dataFolder.OpenStreamForReadAsync("level.txt");
// Read the data.
using (StreamReader streamReader = new StreamReader(file))
{
this.textBlock1.Text = streamReader.ReadToEnd();
}
}
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
await ReadFile();
if (textBlock1.Text == "test")
star2.Visibility = Visibility.Visible;
}