etoisarobot
etoisarobot

Reputation: 7814

Missing something when writing to a Text File

I simply want to be able to log the path of certain files to a text file. I have the following to do the logging.

static void LogFile(string lockedFilePath)
    {
        Assembly ass = Assembly.GetExecutingAssembly();
        string workingFolder = System.IO.Path.GetDirectoryName(ass.Location);
        string LogFile = System.IO.Path.Combine(workingFolder, "logFiles.txt");

        if (!System.IO.File.Exists(LogFile))
        {
            using (System.IO.FileStream fs = System.IO.File.Create(LogFile))
            {
                using (System.IO.StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine(lockedFilePath);   
                }
            }
        }
        else
        {
            using (System.IO.FileStream fs = System.IO.File.OpenWrite(LogFile))
            {
                using (System.IO.StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine(lockedFilePath);
                }
            }
        }
    }

but If I call it in a Console App like this

foreach (string f in System.IO.Directory.GetFiles(@"C:\AASource"))
            {
                Console.WriteLine("Logging : " + f);
                LogFile(f);
            }
            Console.ReadLine();

The only file that is listed in the resulting text file is the last file in the dir. What am I doing wrong?

Upvotes: 1

Views: 1446

Answers (6)

Pankaj
Pankaj

Reputation: 10115

static void LogFile(string lockedFilePath)
        {
            Assembly ass = Assembly.GetExecutingAssembly();
            string workingFolder = System.IO.Path.GetDirectoryName(ass.Location);
            string LogFile = System.IO.Path.Combine(workingFolder, "logFiles.txt");

            System.IO.File.AppendAllText(LogFile, System.Environment.NewLine + lockedFilePath);

        }

Upvotes: 0

Chris Dunaway
Chris Dunaway

Reputation: 11216

You're overwriting the file each time you call the LogFile method. You can use the overload for StreamWriter that allows for appending to the end of the file:

void LogFile(string lockedFilePath)
{
    Assembly ass = Assembly.GetExecutingAssembly();
    string workingFolder = System.IO.Path.GetDirectoryName(ass.Location);
    string LogFile = System.IO.Path.Combine(workingFolder, "logFiles.txt");

    using (System.IO.StreamWriter sw = new StreamWriter(LogFile, true))
    {
        sw.WriteLine(lockedFilePath);   
    }
}

Upvotes: 0

Zaragon
Zaragon

Reputation: 322

File.AppendText MSDN

Upvotes: 0

Jonathan Wood
Jonathan Wood

Reputation: 67345

You need to open the file in append mode. Otherwise, you delete everything that was in the file before.

Here's the logfile code I use if you'd like to see some other examples.

Upvotes: 0

Amedio
Amedio

Reputation: 893

You should Flush and Close the StreamWriter. If only the last file is written there is that you'r just overwriting what you had.

See you.

Upvotes: -1

Rudi Visser
Rudi Visser

Reputation: 22019

Instead of System.IO.File.OpenWrite(LogFile), use System.IO.File.AppendText(LogFile). When you use OpenWrite you're going to be overwriting the contents with whatever you write in.

Additionally, your if statement (if (!System.IO.File.Exists(LogFile))) is not required. AppendText (and OpenWrite) for that matter will create the file if it doesn't exist. This means you can simply get away with running the code within the else clause.

Upvotes: 5

Related Questions