Save Contents Of a TextBox To a File

Nathan Campos picture Nathan Campos · Nov 10, 2009 · Viewed 12.4k times · Source

I'm developing an application that has a TextBox. I want to write its contents to a file, but how can I do this?

Answer

R. Martinho Fernandes picture R. Martinho Fernandes · Nov 10, 2009

There are many ways to accomplish this, the simplest being:

 using(var stream = File.CreateText(path))
 {
      stream.Write(text);
 }

Be sure to look at the MSDN page for File.CreateText and StreamWriter.Write.

If you weren't targeting the .NET Compact Framework, as your tags suggest, you could do even simpler:

 File.WriteAllText(path, string);