1

I want to write in the created text file from content in a textbox after it's been created in the app folder.

I'm getting an error saying that it can't write to it because another process is in use, how can I get around this?

I want the content from textbox3 to be written in the newly made file after it's been created. So the text file isn't there, it's making a text file from the user input on textBox2. I want the input from textBox3 to write in the new file right after it's been made.

Error: `System.IO.IOException: 'The process cannot access the file 'C:\Users\Jason\Desktop\app\x.txt' because it is being used by another process.'

Here's my code below:

private void button1_Click(object sender, EventArgs e)
{
    Console.WriteLine();
    Console.WriteLine("GetFolderPath: {0}",
        Environment.GetFolderPath(Environment.SpecialFolder.System));

    string filename = @"C:\Users\Jason\Desktop\app\" + textBox2.Text + ".txt";
    File.Create(filename);
    File.WriteAllText(filename, textBox3.Text);
}
  • 1
    I wonder from where comes the idea of calling `File.Create` and then `File.WriteAllText`? None of answers here (on SO) will do that, none of tutorials, nor [msdn](https://learn.microsoft.com/en-us/dotnet/standard/io/how-to-write-text-to-a-file). – Sinatr Jul 28 '21 at 13:49
  • Does this answer your question? [Easiest way to read from and write to files](https://stackoverflow.com/questions/7569904/easiest-way-to-read-from-and-write-to-files) – Sinatr Jul 28 '21 at 13:51
  • 1
    You can get the desktop of the current user with `Environment.SpecialFolder.Desktop`. Example: `string filename = Path.Combine(Environment.SpecialFolder.Desktop, @"app\" + textBox2.Text + ".txt");` – Olivier Jacot-Descombes Jul 28 '21 at 13:55
  • I'm meaning that I'm wanting to write text from textBox3 into the newly created text file right after it's been made. I can't do this though because it's "being used by another process" and it's really annoying me. –  Jul 28 '21 at 14:09
  • As Jon Skeet tells you in his answer, drop the line `File.Create(filename);`, because `File.WriteAllText` does this internally already. `WriteAllText` creates the file, writes to it , and finally closes it. – Olivier Jacot-Descombes Jul 28 '21 at 15:12

1 Answers1

4

File.Create returns a FileStream, which will be open until it's disposed.

So you could just dispose of the returned value... but it would be simpler to remove the File.Create call entirely. If the file doesn't exist beforehand, it will be created by the File.WriteAllText call anyway.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Sorry, I'm a complete noob at this, how could I use that to do the same task? Please don't judge me, I'm still learning :) I want to create the file from textBox2 content and then write the text from textBox3 content to the app folder on the desktop. –  Jul 28 '21 at 15:00
  • Yes, that's what I'm suggesting - simply remove the line of code `File.Create(filename);`. That's all you need to do. It will still write to the file on the next line. – Jon Skeet Jul 28 '21 at 15:02
  • I just tried it without that part and the text file wasn't created. I just removed it and nothing else, and it didn't make a .txt file. –  Jul 28 '21 at 15:13
  • You might have to hit the F5 key (for Refresh) on the Desktop to have it the new file displayed. – Olivier Jacot-Descombes Jul 28 '21 at 15:15
  • That code will *definitely* create a file. – Jon Skeet Jul 28 '21 at 15:21
  • Never mind guys, I forgot to change something in it. I accidentally chose the wrong path. Thanks for your help! :) –  Jul 28 '21 at 15:21