Save a string to a text file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CCLeasing

    #1

    Save a string to a text file


    I would like to open a text file titled (c:\logs.txt).
    If the file doesn't exist i would like to create it.
    if it does exist i would like to append to it.

    I would like to insert a line into the end of the file.
    The line is contained in the variable 'line' which is a string
    variable.

    once i've done this i would like to close the file.

  • CCLeasing

    #2
    Save a string to a text file

    thankyou

    Comment

    • Dustin Campbell

      #3
      Re: Save a string to a text file

      I would like to open a text file titled (c:\logs.txt). If the file
      doesn't exist i would like to create it. if it does exist i would like
      to append to it.
      >
      I would like to insert a line into the end of the file.
      The line is contained in the variable 'line' which is a string
      variable.
      once i've done this i would like to close the file.
      Look at the System.IO.File class in the MSDN documentation. It has everything
      that you want.

      private void WriteLinesToFil e(string filePath, string[] lines)
      {
      if (filePath == null || filePath.Length == 0)
      return;
      if (lines == null || lines.Length == 0)
      return;

      StreamWriter fileWriter = null;
      try
      {
      if (File.Exists(fi lePath))
      fileWriter = File.AppendText (filePath);
      else
      fileWriter = File.CreateText (filePath);

      foreach (string line in lines)
      fileWriter.Writ e(line);
      }
      finally
      {
      if (fileWriter != null)
      fileWriter.Clos e();
      }
      }

      Best Regards,
      Dustin Campbell
      Developer Express Inc.


      Comment

      • Mythran

        #4
        Re: Save a string to a text file



        "Dustin Campbell" <dustinc@no-spam-pleasedevexpres s.comwrote in message
        news:c155cb0e17 05f8c8d0776ae20 47c@news.micros oft.com...
        >I would like to open a text file titled (c:\logs.txt). If the file
        >doesn't exist i would like to create it. if it does exist i would like
        >to append to it.
        >>
        >I would like to insert a line into the end of the file.
        >The line is contained in the variable 'line' which is a string
        >variable.
        >once i've done this i would like to close the file.
        >
        Look at the System.IO.File class in the MSDN documentation. It has
        everything that you want.
        >
        private void WriteLinesToFil e(string filePath, string[] lines)
        {
        if (filePath == null || filePath.Length == 0)
        return;
        if (lines == null || lines.Length == 0)
        return;
        >
        StreamWriter fileWriter = null;
        try
        {
        if (File.Exists(fi lePath))
        fileWriter = File.AppendText (filePath);
        else
        fileWriter = File.CreateText (filePath);
        >
        foreach (string line in lines)
        fileWriter.Writ e(line);
        }
        finally
        {
        if (fileWriter != null)
        fileWriter.Clos e();
        }
        }
        >
        Best Regards,
        Dustin Campbell
        Developer Express Inc.
        >
        >
        To modify the posted code:

        StreamWriter fileWriter = null;
        if (File.Exists(fi lePath))
        fileWriter = File.AppendText (filePath);
        else
        fileWriter = File.CreateText (filePath);
        try {
        foreach (string line in lines)
        fileWriter.Writ e(line);
        } finally {
        fileWriter.Clos e();
        }


        Notice, no extra checking in the finally block. If we get that far, the
        file was opened. Since the file is opened right before the try...catch
        block, the fileWriter will contain a reference unless an exception was
        closed, in which case, no writer will be opened (and the reference will
        still be null).

        HTH,
        Mythran


        Comment

        • Dustin Campbell

          #5
          Re: Save a string to a text file

          To modify the posted code:
          >
          StreamWriter fileWriter = null;
          if (File.Exists(fi lePath))
          fileWriter = File.AppendText (filePath);
          else
          fileWriter = File.CreateText (filePath);
          try {
          foreach (string line in lines)
          fileWriter.Writ e(line);
          } finally {
          fileWriter.Clos e();
          }
          Notice, no extra checking in the finally block. If we get that far,
          the file was opened. Since the file is opened right before the
          try...catch block, the fileWriter will contain a reference unless an
          exception was closed, in which case, no writer will be opened (and the
          reference will still be null).
          Of course, at this point it isn't necessary to set "fileWriter " to null at
          all.

          StreamWriter fileWriter;
          if (File.Exists(fi lePath)
          fileWriter = File.AppendText (filePath);
          else
          fileWriter = File.CreateText (filePath);

          try {
          foreach (string line in lines)
          fileWriter.Writ e(line);
          }
          finally {
          fileWriter.Clos e();
          }

          Best Regards,
          Dustin Campbell
          Developer Express Inc.


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Save a string to a text file

            Dustin Campbell <dustinc@no-spam-pleasedevexpres s.comwrote:
            Of course, at this point it isn't necessary to set "fileWriter " to null at
            all.
            >
            StreamWriter fileWriter;
            if (File.Exists(fi lePath)
            fileWriter = File.AppendText (filePath);
            else
            fileWriter = File.CreateText (filePath);
            >
            try {
            foreach (string line in lines)
            fileWriter.Writ e(line);
            }
            finally {
            fileWriter.Clos e();
            }
            Given that AppendText creates the file if it doesn't exist (according
            to the documentation) you can use:

            using (StreamWriter writer = File.AppendText (filePath))
            {
            foreach (string line in lines)
            {
            writer.Write (line);
            }
            }

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • Dustin Campbell

              #7
              Re: Save a string to a text file

              Given that AppendText creates the file if it doesn't exist (according
              to the documentation) you can use:
              >
              using (StreamWriter writer = File.AppendText (filePath))
              {
              foreach (string line in lines)
              {
              writer.Write (line);
              }
              }
              Perfect. Thanks Jon.

              Best Regards,
              Dustin Campbell
              Developer Express Inc.


              Comment

              • Mythran

                #8
                Re: Save a string to a text file



                "Dustin Campbell" <dustinc@no-spam-pleasedevexpres s.comwrote in message
                news:c155cb0e17 0ae8c8d086acf62 604@news.micros oft.com...
                >Given that AppendText creates the file if it doesn't exist (according
                >to the documentation) you can use:
                >>
                >using (StreamWriter writer = File.AppendText (filePath))
                >{
                >foreach (string line in lines)
                >{
                >writer.Write (line);
                >}
                >}
                >
                Perfect. Thanks Jon.
                >
                Best Regards,
                Dustin Campbell
                Developer Express Inc.
                >
                >
                Aye, now if we all combine all our thought processes for our life works, all
                would be good...

                :-)

                Mythran

                Comment

                Working...