Monday, May 20, 2013

Writing Data in log file or text file line by line in C# Asp.Net

private void WriteTextFile()
    {
        string FileLoc = "C:\\LOGFOLDER\\TextFile.txt";

//This will  create the new file if the file is not exist.
        if (!File.Exists(FileLoc))
        {
            using (StreamWriter sw = File.CreateText(FileLoc))
            {
                sw.WriteLine("My Content Data");
            }
        }

//This will append to the existing file if the file already exist.
        else
        {
            using (StreamWriter sw = new StreamWriter(FileLoc, true))
            {
                sw.WriteLine("My Content Data");
            }
        }
    }

No comments:

Post a Comment