如何更改文本文件中的值并覆盖它
本文关键字:覆盖 何更改 文本 文件 | 更新日期: 2023-09-27 18:04:01
我创建了一个*.txt文件,其中包含如下数据:
deviceid,model,availability,renterID,renterName,reneterSurname,OS
0001,iPhone_5S,false,002,John,Dowland,iOS-7.1.2
0002,GalaxyS3,false,002,Amadeus,Mozart,Android-4.3.2
在我的c#应用程序(WPF应用程序)得到这个函数来操作文件:
private void rentSaveButton_Click(object sender, RoutedEventArgs e)
{
StreamWriter sw = new StreamWriter(@"D:'deviceLib.txt", true);
var currentUser = _list.Where(u => u.Id == int.Parse(userIDTextBox.Text)).FirstOrDefault();
var currentDevice = _list2.Where(i => i.deviceId == int.Parse(deviceIDTextBox.Text)).FirstOrDefault();
if (currentDevice != null && currentUser != null)
{
currentDevice.Availability = false;
currentDevice.rentId = currentUser.Id;
currentDevice.rentName = currentUser.Name;
currentDevice.rentSurname = currentUser.Surname;
dataGridDeviceList.Items.Refresh();
sw.WriteLine();
sw.Close();
MessageBox.Show("Rent done. Thanks!");
tabControl.SelectedItem = mainTab;
}
else
{
MessageBox.Show("We don't have such device. Sorry :( ");
userIDTextBox.Clear();
deviceIDTextBox.Clear();
}
}
现在,问题是我通常可以在程序工作期间处理这个文件,它改变值,可以读取它等,但当我关闭应用程序(通过X按钮)什么也没有发生。
您没有向文件写入任何内容。
sw.WriteLine(); // you need to pass a string in to this function
在我看来,你一定是发明了一个理论,你对currentDevice
所做的任何改变都应该以某种方式影响到写入sw
的内容。你在这里看到的就是卡尔·波普尔(Karl Popper)过去所说的"失证":你的理论预测了可以观察到的行为;你观察;行为没有发生。第一种解释是你的理论是错误的。
事实上,这是对你所看到(没有看到)的东西的正确解释。currentDevice
和sw
之间绝对没有任何联系。一个也没有。这就像你在按微波炉上的按钮,试图启动你的车。如果你不换一条路,你就得走路去上班了。
currentDevice.Availability = false;
currentDevice.rentId = currentUser.Id;
currentDevice.rentName = currentUser.Name;
currentDevice.rentSurname = currentUser.Surname;
dataGridDeviceList.Items.Refresh();
sw.WriteLine();
sw.Close();
你所做的就是在一个随机对象上改变一堆属性,刷新你的数据网格,然后写一个换行符, 一个换行符,没有其他,到输出流。为什么没有参数的sw.WriteLine();
会这样做?好吧,它这样做是因为这是设计师决定的。理智地不能做任何其他事情,因为你没有给它任何可以写的东西。这种行为是有记录的,如果你花十秒钟阅读文档,你就会知道。
如果你想写一个非空的行到一个文件中,使用WriteLine
的许多重载中的一个,这些重载在文档中有详细的说明。如果您只想编写一行的部分,请使用Write
的许多重载之一,这些重载也有文档记录。
这样的东西会很好(我猜你的字段名;如果你不知道它们是什么,也许其他人在StackOverflow上知道你的代码的那部分看起来像什么,可以帮助你):
currentDevice.Availability = false;
currentDevice.rentId = currentUser.Id;
currentDevice.rentName = currentUser.Name;
currentDevice.rentSurname = currentUser.Surname;
// Write fields in desired order of appearance in the file.
sw.Write(currentDevice.deviceID);
// There are many far superior ways to write a comma to the file,
// and I'll hear about all of them in comments, but we're keeping
// it as simple as possible for the moment.
sw.Write(",");
sw.Write(currentDevice.model);
sw.Write(",");
// Properties you just set
sw.Write(currentDevice.Availability);
sw.Write(",");
sw.Write(currentDevice.rentID);
sw.Write(",");
sw.Write(currentDevice.rentName);
sw.Write(",");
sw.Write(currentDevice.rentSurname);
sw.Write(",");
sw.Write(currentDevice.OS);
// NOW write a newline.
sw.WriteLine();
但那是丑陋的。我们把它卷进一个方法来隐藏它。这被称为"重构"。
public void WriteDeviceStateToStream(StreamWriter stream, WhateverTheDeviceClassIs device)
{
// Write fields in desired order of appearance in the file.
stream.Write(device.deviceID);
// There are many far superior ways to write a comma to the file,
// and I'll hear about all of them in comments, but we're keeping
// it as simple as possible for the moment.
stream.Write(",");
stream.Write(device.model);
stream.Write(",");
// Properties you just set
stream.Write(device.Availability);
stream.Write(",");
stream.Write(device.rentID);
stream.Write(",");
stream.Write(device.rentName);
stream.Write(",");
stream.Write(device.rentSurname);
stream.Write(",");
stream.Write(device.OS);
// NOW write a newline.
stream.WriteLine();
// DO NOT close the stream here. The stream belongs to the caller; make no
// assumptions about what he plans to do with it next.
}
…在事件处理程序中,调用那个方法。还要注意StreamWriter
的using
语句。这样就可以正确地处理它,从而关闭文件等等。这很重要。
private void rentSaveButton_Click(object sender, RoutedEventArgs e)
{
using (StreamWriter sw = new StreamWriter(@"D:'deviceLib.txt", true))
{
var currentUser = _list.Where(u => u.Id == int.Parse(userIDTextBox.Text)).FirstOrDefault();
var currentDevice = _list2.Where(i => i.deviceId == int.Parse(deviceIDTextBox.Text)).FirstOrDefault();
if (currentDevice != null && currentUser != null)
{
currentDevice.Availability = false;
currentDevice.rentId = currentUser.Id;
currentDevice.rentName = currentUser.Name;
currentDevice.rentSurname = currentUser.Surname;
dataGridDeviceList.Items.Refresh();
// Call write method
WriteDeviceStateToStream(sw, currentDevice);
// The user's going to get real tired of this messagebox real fast.
MessageBox.Show("Rent done. Thanks!");
tabControl.SelectedItem = mainTab;
}
else
{
MessageBox.Show("We don't have such device. Sorry :( ");
userIDTextBox.Clear();
deviceIDTextBox.Clear();
}
}
}