8

I want to create a text file then add the text of a TextBox to it. Creating the text file works without any problems with following code:

InitializeComponent();
string path = @"C:\Users\Morris\Desktop\test.txt";
if (!File.Exists(path))
{
    File.Create(path);
}

But I get an error that the file is being used when I try to add the text to the text file. If the file already exist before it run the code I don't get this error and the TextBox.Text is added to the File. I use this code to add the text to the text file:

public void writeTxt()
{
    string path = @"C:\Users\Morris\Desktop\test.txt";
    if (File.Exists(path))
    {
        using (var tw = new StreamWriter(path, true))
        {
            tw.WriteLine(TextBox1.Text);
            tw.Close();
        }
    }
}

Can you help me?

mrogal.ski
  • 5,828
  • 1
  • 21
  • 30
Morris
  • 159
  • 1
  • 2
  • 10

6 Answers6

18

You don't actually have to check if the file exists, as StreamWriter will do that for you.

using (var tw = new StreamWriter(path, true))
{
    tw.WriteLine(TextBox1.Text);
}

public StreamWriter( string path, bool append )

Determines whether data is to be appended to the file. If the file exists and append is false, the file is overwritten. If the file exists and append is true, the data is appended to the file. Otherwise, a new file is created.

Hadi Farhadi
  • 1,773
  • 2
  • 16
  • 33
  • Okay, thanks for that. I'm changing my code from the ground up so there is still a lot of irrelevant code to be removed. But I did not know that StreamWriter will check it for me. – Morris Jul 24 '17 at 09:51
  • 1
    yes, `Otherwise, a new file is created.` – Hadi Farhadi Jul 24 '17 at 09:53
  • Perfect I had the StramWritter in place but I wasn't sure it will create or writte if there is already there, seems I didn't need to control that! nice and thanks! – Juano Sep 01 '20 at 15:30
2

You should use File.Create with using statement as it's locking the file on creating.So just change this line :

File.Create(path);

To this:

using (File.Create(path));
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
0

If the file exists, it can be either overwritten or appended to. If the file does not exist, this constructor creates a new file. So you don't need to check if the file exists or not.

You need to make sure the file is closed before you want to modify it.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
jerry
  • 317
  • 2
  • 20
0

As you can see here, StreamWriter will actually create a file on specified path when it doesn't exist so it's useless to check for it.

I would suggest to remove the part which is creating the file and simply just start writing :

public void writeTxt()
{
    string path = @"C:\Users\Morris\Desktop\test.txt";
    using (var tw = new StreamWriter(path, true))
    {
        tw.WriteLine(TextBox1.Text);
        tw.Close();
    }
}

But if you really want to create the file beforehand, remember to Dispose FileStream object created by File.Create call. Dispose call will automatically call Flush and Close for you so it's safe enough and you can do this in many ways like this :

InitializeComponent();
string path = @"C:\Users\Morris\Desktop\test.txt";
if (!File.Exists(path))
{
    using ( File.Create(path) ) ; // This will generate warnings that you're not using the object and so on, but that's okay,
}

Or like this :

InitializeComponent();
string path = @"C:\Users\Morris\Desktop\test.txt";
if (!File.Exists(path))
{
    FileStream fs = File.Create(path);
    fs.Dispose();
}
mrogal.ski
  • 5,828
  • 1
  • 21
  • 30
-1

You need to Move your

tw.Close();

Outside your using. Like so :

public void writeTxt()
{
    string path = @"C:\Users\Morris\Desktop\test.txt";
    if (File.Exists(path))
    {
        using (var tw = new StreamWriter(path, true))
        {
            tw.WriteLine(TextBox1.Text);
        }
        tw.Close();
    }
}

Edit : As pointed out, when the using ends the writer is disposed, so does not need manually closing.

public void writeTxt()
{
    string path = @"C:\Users\Morris\Desktop\test.txt";
    if (File.Exists(path))
    {
        using (var tw = new StreamWriter(path, true))
        {
            tw.WriteLine(TextBox1.Text);
        }
    }
}
Tommy
  • 1
  • 1
-2

This problem has been answered before in this thread. Closing a file after File.Create

You will need to close the stream to the file before using it again.

Thugge
  • 208
  • 1
  • 4
  • 10