So not sure why this does not work. I have this in a button on my web form.
protected void btnSave_Click(object sender, EventArgs e)
{
using (StreamWriter sw = new StreamWriter("C:\\temp\\test.txt"))
{
sw.Flush();
sw.WriteLine(txtDescription.Text);
}
}
When I type something into that text box and click the save button, nothing happens. No matter if the file exists ahead of time or not.
Not really sure what I missed. I have tried closing the streamwriter after the write line but that doesn't seem to work either. I am wondering if there is something in my form that needs to be changed.
If there is no exception this code will work. This means that you are making a mistake not shown here. Maybe you are looking at the wrong drive. Or, you are not noticing exceptions (swallowing them somehow). Maybe txtDescription.Text
does not contain what you expect?! Single-step through this code with the debugger and check for that file immediately after sw
has been disposed.
Although I cannot tell you the specific mistake I can tell you to look elsewhere and to make sure that you are correctly interpreting what you see.
It's simpler to call File.WriteAllText
but that will not fix the mistake.