1.11M Members

Create a txt file by C#


Hello All :

Is there any idea to create a txt file by C#, I want it to save all the status of controls of the form inside it????

Thanks for support


Here is a simple general way to write to a text file:

// FileWrite - write input from the Console into a text file

using System;
using System.IO;

namespace FileWrite
  public class Class1
    public static void Main(string[] args)
      // create the filename object - the while loop allows
      // us to keep trying with different filenames until
      // we succeed
      StreamWriter sw = null;
      string sFileName = "";
          // enter output filename (simply hit Enter to quit)
          Console.Write("Enter filename "
                      + "(Enter blank filename to quit):");
          sFileName = Console.ReadLine();
          if (sFileName.Length == 0)
            // no filename - this jumps beyond the while
            // loop to safety

          // open file for writing; throw an exception if the
          // file already exists:
          //   FileMode.CreateNew to create a file if it
          //                   doesn't already exist or throw
          //                   an exception if file exists
          //   FileMode.Append to create a new file or append
          //                   to an existing file
          //   FileMode.Create to create a new file or 
          //                   truncate an existing file

          //   FileAccess possibilities are:
          //                   FileAccess.Read, 
          //                   FileAccess.Write,
          //                   FileAccess.ReadWrite
          FileStream fs = File.Open(sFileName, 

          // generate a file stream with UTF8 characters
          sw = new StreamWriter(fs, System.Text.Encoding.UTF8);

          // read one string at a time, outputting each to the
          // FileStream open for writing
          Console.WriteLine("Enter text; enter blank line to stop");
            // read next line from Console;
            // quit if line file is blank
            string sInput = Console.ReadLine();
            if (sInput.Length == 0)

            // write the line just read to output file

          // close the file we just created
          sw = null;
        catch(IOException fe)
          // whoops -- an error occurred somewhere during
          // the processing of the file - tell the user 
          // what the full name of the file is:
          // take the path name onto the filename
          string sDir = Directory.GetCurrentDirectory();
          string s = Path.Combine(sDir, sFileName);
          Console.WriteLine("Error on file {0}", s);

          // now output the error message in the exception

      // wait for user to acknowledge the results
      Console.WriteLine("Press Enter to terminate...");

Thank you very much ,,,, but I was wonder if I have a form inside it checkbox textbox ...etc while I want to press my save button I want a directly text file created in any partition (without save dialog) saving the status of those controls ....so can I do it in C#??????

thanks for support....


Extract the generated code by VS IDE, save it to the text file.


but, if you need to save some properties of the controls, I recommend to use .settings file


Hello :

Thanks a lot , but how can I create s setting file can you explain please ???


Project->add new item->Settings file

This article has been dead for over six months: Start a new discussion instead
Start New Discussion
Tags Related to this Article