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?