在我看来,您一定是发明了一个理论,即您对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();,没有参数这样做?嗯,它这样做是因为设计师决定这样做。它没有其他任何东西可以 做,因为你没有给它任何可写的东西。并且记录了该行为,如果您花费了十秒钟 reading the documentation,您就会知道这一点。
如果您想将非空行写入文件,请使用众多overloads of WriteLine which are documented in the documentation 之一。如果您只想写一行的部分,请使用the many overloads of Write, which are also documented 之一。
这样就可以了(我猜你的字段名称;如果你不知道它们是什么,也许 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();
}
}
}