Categories

Tools

bitdefender

1&1 Web Hosting

Use the StreamWriter class to write a text file using C#


A simple example of how to use the StreamWriter class to write a text file using C#.

Code:

using System;
using System.IO;
using System.Threading;

namespace ConsoleApplications
{
    class StreamWriterDemo
    {
        private static string pathFile = "log.txt";

        static void Main(string[] args)
        {
            string logTimestamp = DateTime.Now.ToString();
            string logEvent = "Some events that occurred in the system...";

            WriteLog(logTimestamp, logEvent);
            Console.WriteLine("An event has been posted to the log file");

            Console.WriteLine("We look forward to the next event ....");
            Thread.Sleep(5000);

            logTimestamp = DateTime.Now.ToString();
            WriteLog(logTimestamp, logEvent);
            Console.WriteLine("An event has been posted to the log file");

            Console.WriteLine("We look forward to the next event ....");
            Thread.Sleep(10000);

            logTimestamp = DateTime.Now.ToString();
            WriteLog(logTimestamp, logEvent);
            Console.WriteLine("An event has been posted to the log file");

            Console.WriteLine("We look forward to the next event ....");
            Thread.Sleep(3000);

            logTimestamp = DateTime.Now.ToString();
            WriteLog(logTimestamp, logEvent);
            Console.WriteLine("An event has been posted to the log file");
        }

        /// <summary>
        /// Adds a new line to the log file, adding logTimestamp and logEvent
        /// </summary>
        /// <param name="logTimestamp"></param>
        /// <param name="logEvent"></param>
        /// <returns></returns>
        private static bool WriteLog(string logTimestamp, string logEvent)
        {
            bool result = true;

            FileStream fs = null;
            StreamWriter sw = null;
            try
            {
                fs = File.Open(pathFile, FileMode.Append, FileAccess.Write);
                sw = new StreamWriter(fs);
                sw.Write(logTimestamp + Environment.NewLine + logEvent + Environment.NewLine);
                result = true;
                sw.Close();
                fs.Close();
            }
            catch (Exception excp)
            {
                result = false;
                sw.Close();
                fs.Close();
                Console.WriteLine("Error:" + excp.ToString());
            }
            finally
            {
                result = false;
                sw.Close();
                fs.Close();
            }
            return result;
        }
    }
}

 

Note: This example is also used Thread.Sleep(x); to pause for x milliseconds running the program.

Posted in C# by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.