如何更改txt文件中的值并覆盖它

时间:2016-08-25 12:50:16

标签: c# file text overwrite

我已经使用以下数据创建了* .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#app(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按钮)没有任何反应。 TXT文件不受影响,也不保存任何更改。

2 个答案:

答案 0 :(得分:2)

您没有在文件中写任何内容。

sw.WriteLine(); // you need to pass a string in to this function

答案 1 :(得分:1)

在我看来,您必须发明一种理论,即您对currentDevice所做的任何更改都应该以某种方式影响写入sw的内容。你在这里看到的是卡尔波普尔曾经称之为“不确认”的东西:你的理论预测了可以观察到的行为;你观察;行为没有发生。对此的第一个解释是你的理论是错误的。

这实际上是对你(不)看到的正确解释。 currentDevicesw之间绝对没有任何联系。没有。这就像你按下微波炉上的按钮以开始你的车一样。如果你不切换到另一种方法,你将走路去上班。

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上的其他人知道代码的那部分是什么样的,可以帮助你): / p>

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. 
}

...在您的事件处理程序中,调用该方法。另请注意using的{​​{1}}声明。正确处理它,关闭文件等等。这很重要。

StreamWriter