简体   繁体   English

将数据从文本框写入文本文件

[英]Writing data from textbox into text file

Here is the code im using to write and read from text file. 这是代码im用于读写文本文件。

StreamWriter sw1 = new StreamWriter("DataNames.txt");
sw1.WriteLine(textBox1.Text);
sw1.Close();
StreamWriter sw2 = new StreamWriter("DataNumbers.txt");
sw2.WriteLine(textBox2.Text);
sw2.Close();
FileInfo file1 = new FileInfo("DataNames.txt");
StreamReader sr1 = file1.OpenText();
while (!sr1.EndOfStream)
{
    listBox1.Items.Add(sr1.ReadLine());
}
FileInfo file2 = new FileInfo("DataNumbers.txt");
StreamReader sr2 = file2.OpenText();
while (!sr2.EndOfStream)
{
    listBox2.Items.Add(sr2.ReadLine());
}

The thing is that when I click my button to save data from my textboxes to my text files an error appears that says "The process cannot access the file 'C:\\xxxx\\xxxxxx\\xxxxx\\xxxx\\xxxxx\\xxxxx.txt' because it is being used by another process." 问题是,当我单击按钮以将文本框中的数据保存到文本文件时,出现错误,提示“该进程无法访问文件'C:\\ xxxx \\ xxxxxx \\ xxxxx \\ xxxx \\ xxxxx \\ xxxxx.txt”,因为它正在被另一个进程使用。”

Can anyone tell me why I have this error and maybe help me fix it 谁能告诉我为什么我有这个错误,也许可以帮助我解决它

Try added a using statment around your streams to make sure they are Disposed otherwise the file is still locked to the stream 尝试在流周围添加一个using语句,以确保已将它们Disposed否则文件仍被锁定到流

Example: 例:

   //Write
    using (StreamWriter sw1 = new StreamWriter("DataNames.txt"))
    {
        sw1.WriteLine(textBox1.Text);
    }

    using (StreamWriter sw2 = new StreamWriter("DataNumbers.txt"))
    {
        sw2.WriteLine(textBox2.Text);
    }


    // Read
    foreach (var line in File.ReadAllLines("DataNames.txt"))
    {
        listBox1.Items.Add(line);
    }

    foreach (var line in File.ReadAllLines("DataNumbers.txt"))
    {
        listBox2.Items.Add(line);
    }

It appears you do not close the file after you read it. 似乎您在读取文件后没有关闭文件。 After you call FileInfo.OpenText you get a StreamReader which has to be closed, either via Close method, or even better, with a using statement. 调用FileInfo.OpenText之后,您将获得一个StreamReader ,必须通过Close方法关闭 ,甚至更好的是,使用using语句将其关闭。

But there are already methods that do all that for you, have a look at File.WriteAllText , File.AppendAllText and File.ReadAllLines methods. 但是已经有可以为您完成所有操作的方法,请看看File.WriteAllTextFile.AppendAllTextFile.ReadAllLines方法。

You need to Close the StreamReader object once you do not need it any more. 一旦不再需要,就需要关闭 StreamReader对象。 This should fix this issue. 这应该可以解决此问题。 Ie

StreamReader sr1 = file1.OpenText();
try {
            while (!sr1.EndOfStream)
            {
                listBox1.Items.Add(sr1.ReadLine());
            }
}
finally { 
  sr1.Close();
}
            FileInfo file2 = new FileInfo("DataNumbers.txt");
            StreamReader sr2 = file2.OpenText();
try {
            while (!sr2.EndOfStream)
            {
                listBox2.Items.Add(sr2.ReadLine());
            }
}
finally {
  sr2.Close();
}

You have opened files but not closed. 您已打开文件但尚未关闭。

StreamReader sr1 = file1.OpenText();
StreamReader sr2 = file2.OpenText();

Your problem occurs, because you are not closing the stream readers. 发生您的问题,因为您没有关闭流阅读器。

A safer way of using external resources (the files in this case) is to embed their use in a using statement. 使用外部资源(在这种情况下为文件)的一种更安全的方法是将其使用嵌入using语句中。 The using statement automatically closes the resource at the end of the statement block or if the statement block if left in another way. using语句在语句块的末尾或如果语句块以其他方式离开时自动关闭资源。 This could be a return statement or an exception, for instance. 例如,这可能是return语句或异常。 It is guaranteed that the resource will be closed, even after an exception occurs. 即使发生异常,也可以保证关闭资源。

You can apply the using statement on any object which implements the IDisposable interface. 您可以将using语句应用于实现IDisposable接口的任何对象。

// Writing to the files
using (var sw1 = new StreamWriter("DataNames.txt")) {
    sw1.WriteLine(textBox1.Text);
}
using(var sw2 = new StreamWriter("DataNumbers.txt")) {
    sw2.WriteLine(textBox2.Text);
}

// Reading from the files
FileInfo file1 = new FileInfo("DataNames.txt");
using (StreamReader sr1 = file1.OpenText()) {
    while (!sr1.EndOfStream) {
        listBox1.Items.Add(sr1.ReadLine());
    }
}
FileInfo file2 = new FileInfo("DataNumbers.txt");
using (StreamReader sr2 = file2.OpenText()) {
    while (!sr2.EndOfStream)
    {
        listBox2.Items.Add(sr2.ReadLine());
    }
}

However, you can simplify the reading part like this 但是,您可以像这样简化阅读部分

// Reading from the files
listBox1.Items.AddRange(File.ReadAllLines("DataNames.txt"));
listBox2.Items.AddRange(File.ReadAllLines("DataNumbers.txt"));

I've seen this behavior before - usually there's another process open that's blocking the file access. 我以前已经看到过这种行为-通常有另一个打开的进程正在阻止文件访问。 Do you have multiple development servers open in your taskbar? 您是否在任务栏中打开了多个开发服务器? (Strange, yes, but I've seen it happen) (很奇怪,是的,但是我已经看到了这种情况)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM