1,105,416 Community Members

Create a txt file by C#

Member Avatar
aminit
Junior Poster in Training
78 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Lardmeister
Posting Virtuoso
1,966 posts since Mar 2007
Reputation Points: 434 [?]
Q&As Helped to Solve: 111 [?]
Skill Endorsements: 8 [?]
 
0
 

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 = "";
      while(true)
      {
        try
        {
          // 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
            break;
          }

          // 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, 
                                    FileMode.CreateNew, 
                                    FileAccess.Write);

          // 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");
          while(true)
          {
            // read next line from Console;
            // quit if line file is blank
            string sInput = Console.ReadLine();
            if (sInput.Length == 0)
            {
              break;
            }

            // write the line just read to output file
            sw.WriteLine(sInput);
          }

          // close the file we just created
          sw.Close();
          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
          Console.WriteLine(fe.Message);
        }
      }

      // wait for user to acknowledge the results
      Console.WriteLine("Press Enter to terminate...");
      Console.Read();
    }
  }
}
Member Avatar
aminit
Junior Poster in Training
78 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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....

Member Avatar
Ramy Mahrous
Postaholic
2,189 posts since Aug 2006
Reputation Points: 401 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
Featured
 
0
 

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

Member Avatar
Ramy Mahrous
Postaholic
2,189 posts since Aug 2006
Reputation Points: 401 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
Featured
 
0
 

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

Member Avatar
aminit
Junior Poster in Training
78 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello :

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

Member Avatar
Ramy Mahrous
Postaholic
2,189 posts since Aug 2006
Reputation Points: 401 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
Featured
 
0
 

Project->add new item->Settings file

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