Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7828493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:22:20+00:00 2026-06-02T10:22:20+00:00

This should be an extremely easy fix, but for some reason I am missing

  • 0

This should be an extremely easy fix, but for some reason I am missing something. All I am trying to do is get the String Builder function that I have to write the header, but for some reason it isnt currently.

When I try to change the if statement to !File.Exists(tempFileName), it does not run through my loop.

Any suggestions? Also, let me know if you need more info. Thanks in advance.

public static void Open(string tempFileName, string division,
                             int zipFiles, int conversions, int returnedFiles, int totalEmails)
    {
        StreamWriter dailyStats;

        //This is where I am missing something
        //I am passing in the original filename of a log, then adding "-Stats.log"
        //so I can tell the difference between what is the new stats file, and the original log file
        if (File.Exists(tempFileName))
        {
            dailyStats = new StreamWriter(tempFileName + "-Stats.log");

            StringBuilder sb = new StringBuilder();
            sb.Append("Division");
            sb.Append("\t");
            sb.Append("Zip Files");
            sb.Append("\t");
            sb.Append("Conversions");
            sb.Append("\t");
            sb.Append("Returned Files");
            sb.Append("\t");
            sb.Append("Total E-Mails");
            sb.Append("\t");

        }
        else
        {
            dailyStats = File.AppendText(tempFileName + "-Stats.log");
        }

        if (writeLog)
        {
            //Use a string builder to assemble the content for performance reasons
            StringBuilder s = new StringBuilder();
            s.Append(division);
            s.Append("\t");
            s.Append(zipFiles);
            s.Append("\t");
            s.Append(conversions);
            s.Append("\t");
            s.Append(returnedFiles);
            s.Append("\t");
            s.Append(totalEmails);
            s.Append("\t");

            dailyStats.WriteLine(s.ToString());
        }

        dailyStats.Close();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:22:22+00:00Added an answer on June 2, 2026 at 10:22 am

    You can fix it like this

    var sb = new StringBuilder();
    string logFileName = tempFileName + "-Stats.log";
    if (File.Exists(logFileName)) {
        dailyStats = File.AppendText(logFileName); 
    } else {
        dailyStats = new StreamWriter(logFileName); 
    
        // Write header
        sb.Append("Division");
        ...
        sb.AppendLine();
    } 
    
    if (writeLog)  {
        sb.Append(division);
        ...
        dailyStats.WriteLine(sb.ToString());
    } 
    dailyStats.Close();
    

    UPDATE

    The code had different errors. Two StringBuilders were created, but only one was written to the file. The existence of the file was determined for a different file name than the actual file that was written to. And finally, the logic depending on the existence of the file was inverted. I rewrote and refactored the code completely, in order to make it more understandable and manageable

    public static void Open(string tempFileName, string division,
                         int zipFiles, int conversions, int returnedFiles, int totalEmails)
    {
        if (!writeLog)
            return;
    
        using (StreamWriter dailyStats = OpenLogFile(tempFileName)) {
            var sb = new StringBuilder();
            sb.Append(division);
            // ...
            dailyStats.WriteLine(sb.ToString());
        }
    }
    
    private static StreamWriter OpenLogFile(string tempFileName)
    {
        StreamWriter dailyStats;
        string logFileName = tempFileName + "-Stats.log";
        if (File.Exists(logFileName)) {
            dailyStats = File.AppendText(logFileName);
        } else {
            dailyStats = new StreamWriter(logFileName);
            WriteHeader(dailyStats);
        }
        return dailyStats;
    }
    
    private static void WriteHeader(StreamWriter dailyStats)
    {
        var sb = new StringBuilder();
        sb.Append("Division");
        // ...
        dailyStats.WriteLine(sb.ToString());
    }
    

    Note: The using statement closes the file and releases the external resources automatically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be really easy but for some reason it doesn't seem to be.
This should be a really really simple thing, but for some reason it is
This should a quick question for some easy rep. I'm doing some PHP Website
This should be easy for many of you, but for me it's just another
This should be easy, but I'm not sure how to best go about it.
The question is extremely easy, but the solution might not. Let's say this is
This should be a simple question, but I haven't been able to find a
This is one of this things that should be extremely simple and I just
This seems like it should be extremely simple; however, after two hours of reading
As title, I am confused about this. swap should be extremely useful if we

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.