SIGN UP MEMBER LOGIN:    
Resource

How to append a text file in C#

Soft Dev Resources Jul 06, 2009
How to append a text or log file in C#
This code snippet shows how to append a text file in C#.

using System;

using System.IO;

 

 

namespace FileOperationsSample

{

    class Program

    {

 

        private static string logFileName = @"C:\Junk\MyLogfile.txt";

 

        public static void Main(String[] args)

        {

            using (StreamWriter writer = File.AppendText(logFileName))

            {

                Log("Test1", writer);

                Log("Test2", writer);

                writer.Close();

            }

            // Open and read the file.

            using (StreamReader reader = File.OpenText(logFileName))

            {

                DumpLog(reader);

            }

 

            Console.ReadLine();

        }

        public static void Log(String logMessage, TextWriter writer)

        {

            writer.Write("\r\nLog Entry : ");

            writer.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),

                DateTime.Now.ToLongDateString());

            writer.WriteLine("  :");

            writer.WriteLine("  :{0}", logMessage);

            writer.WriteLine("-------------------------------");

            // Update the underlying file.

            writer.Flush();

        }

        public static void DumpLog(StreamReader reader)

        {

            // While not at the end of the file, read and write lines.

            String line;

            while ((line = reader.ReadLine()) != null)

            {

                Console.WriteLine(line);

            }

            reader.Close();

        }

    }

 

}

 


share this resource :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Sponsored by
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor