写入和读取文本文件-C#Windows通用平台应用程序Windows 10
本文关键字:平台 应用程序 Windows -C#Windows 读取 取文本 文件 | 更新日期: 2023-09-27 18:28:27
********它起作用了!但是,在显示任何内容之前,您必须在文本框中键入内容。我想这是因为我使用了"TextChanged"事件处理程序。如果我希望它在没有用户交互的情况下显示文本文件的内容,我应该使用哪个事件处理程序?********
因此,当按下按钮时,我想在C#Windows 10通用平台应用程序中的文本文件中写入一些数据,并且我想要一个TextBlock或TextBox来读取我的应用程序中该文本文件的内容。
我使用的是一个透视风格的应用程序,写文件的按钮在一个透视图上,我想包含文本文件内容的TextBlock或TextBox在另一个透视表上。
我的代码在下面。它不起作用。我甚至不确定它是否在创建和编写文件,而且我的TextBox或TextBlock中什么都没有。:(
我从这里得到代码:https://msdn.microsoft.com/en-us/library/windows/apps/mt185401.aspx
写入文件的代码:
private async void submitButton_Click(object sender, RoutedEventArgs e)
{
//WRITE THE TICKET TO A LOCAL DATABASE (txt)//
//Create the text file to hold the data
Windows.Storage.StorageFolder storageFolder =
Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile ticketsFile =
await storageFolder.CreateFileAsync("tickets.txt",
Windows.Storage.CreationCollisionOption.ReplaceExisting);
//Write data to the file
await Windows.Storage.FileIO.WriteTextAsync(ticketsFile, "Swift as a shadow");
}
读取文本框中文件的代码:
private async void viewTickets_TextChanged(object sender, TextChangedEventArgs e)
{
Windows.Storage.StorageFolder storageFolder =
Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile ticketsFile =
await storageFolder.GetFileAsync("tickets.txt");
string savedTickets = await Windows.Storage.FileIO.ReadTextAsync(ticketsFile);
viewTickets.Text = savedTickets;
}
您的代码非常好,唯一的问题是无法执行。当您单击button
时,您的文件即被创建。但是您没有在textbox
中键入任何内容,因此您永远不会读取该文件。
我想你想在写完后立即阅读。把你的Read
文件代码放在Write
代码后面:
private async void submitButton_Click(object sender, RoutedEventArgs e)
{
//WRITE THE TICKET TO A LOCAL DATABASE (txt)//
//Create the text file to hold the data
Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile ticketsFile = await storageFolder.CreateFileAsync("tickets.txt", Windows.Storage.CreationCollisionOption.ReplaceExisting);
//Write data to the file
await Windows.Storage.FileIO.WriteTextAsync(ticketsFile, "Swift as a shadow");
//read file
string savedTickets = await Windows.Storage.FileIO.ReadTextAsync(ticketsFile);
viewTickets.Text = savedTickets;
}
并移除CCD_ 5事件处理程序。
public async Task WriteToDictionaryAsync(Dictionary<string, string> dictionary, StorageFile binarySourceFile)
{
await Task.Delay(5);
using (FileStream fs = File.OpenWrite(binarySourceFile.Path))
using (BinaryWriter writer = new BinaryWriter(fs))
{
// Put count.
writer.Write(dictionary.Count);
// Write pairs.
foreach (var pair in dictionary)
{
writer.Write(pair.Key);
writer.Write(pair.Value);
}
}
}
public async void WriteToDictionary(Dictionary<string, string> dictionary, StorageFile binarySourceFile)
{
await Task.Delay(5);
using (FileStream fs = File.OpenWrite(binarySourceFile.Path))
using (BinaryWriter writer = new BinaryWriter(fs))
{
// Put count.
writer.Write(dictionary.Count);
// Write pairs.
foreach (var pair in dictionary)
{
writer.Write(pair.Key);
writer.Write(pair.Value);
}
}
}
public async Task<Dictionary<string, string>> ReadDictionaryAsync(StorageFile binarySourceFile)
{
await Task.Delay(5);
var result = new Dictionary<string, string>();
using (FileStream fs = File.OpenRead(binarySourceFile.Path))
using (BinaryReader reader = new BinaryReader(fs))
{
// Get count.
int count = reader.ReadInt32();
// Read in all pairs.
for (int i = 0; i < count; i++)
{
string key = reader.ReadString();
string value = reader.ReadString();
result[key] = value;
}
}
return result;
}
public Dictionary<string, string> ReadDictionary(StorageFile binarySourceFile)
{
var result = new Dictionary<string, string>();
using (FileStream fs = File.OpenRead(binarySourceFile.Path))
using (BinaryReader reader = new BinaryReader(fs))
{
// Get count.
int count = reader.ReadInt32();
// Read in all pairs.
for (int i = 0; i < count; i++)
{
string key = reader.ReadString();
string value = reader.ReadString();
result[key] = value;
}
}
return result;
}
//创建bin文件var binarySourceFile=await StorageData.LocalFolder.CreateFileAsync("binFile"+".bin",CreationCollisionOption.ReplaceExisting);