BorisTheBlade
BorisTheBlade

Reputation: 51

Writing data from textbox into text file

Here is the code im using to write and read from text file.

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

Can anyone tell me why I have this error and maybe help me fix it

Upvotes: 1

Views: 10255

Answers (6)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112762

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. The using statement automatically closes the resource at the end of the statement block or if the statement block if left in another way. This could be a return statement or an exception, for instance. 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.

// 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"));

Upvotes: 1

Patko
Patko

Reputation: 4423

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.

But there are already methods that do all that for you, have a look at File.WriteAllText, File.AppendAllText and File.ReadAllLines methods.

Upvotes: 3

sa_ddam213
sa_ddam213

Reputation: 43636

Try added a using statment around your streams to make sure they are Disposed otherwise the file is still locked to the stream

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);
    }

Upvotes: 4

Tim
Tim

Reputation: 4099

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)

Upvotes: 0

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33391

You have opened files but not closed.

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

Upvotes: 1

platon
platon

Reputation: 5340

You need to Close the StreamReader object once you do not need it any more. This should fix this issue. I.e.

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();
}

Upvotes: 1

Related Questions