Him_Jalpert
Him_Jalpert

Reputation: 2516

stringbuilder or streamwriter overwriting information

I'm sending a couple of dictionaries to a streamwriter and a stringbuilder, only one of the dictionary's info is displayed in my output file though. I'm not sure what to do to get everything to output at once, code is below, thanks!

public void RunReport()
{
    CSVProfileCreate(genderKeys, genderValues);
    CSVProfileCreate(ageKeys, ageValues);
}

public void CSVProfileCreate<T>(Dictionary<T, string> columns, Dictionary<T, int> data)
{
    using (StreamWriter write = new StreamWriter("c:/temp/testoutputprofile.csv"))
    {
        StringBuilder output = new StringBuilder();

        IEnumerable<string> col = columns.Values.AsEnumerable();
        IEnumerable<int> dat = data.Values.AsEnumerable();

        for (int i = 0; i < col.Count(); i++)
        {
            output.Append(col.ElementAt(i));
            output.Append(",");
            output.Append(dat.ElementAt(i));
            output.Append(",");
            output.Append(Environment.NewLine);
        }

        write.Write(output);
    }
}

Upvotes: 1

Views: 924

Answers (1)

aqwert
aqwert

Reputation: 10789

Try calling

write.Flush();

after the write.

EDIT:

Sorry, just read your question again,

try:

using (StreamWriter write = new StreamWriter("c:/temp/testoutputprofile.csv", true))

Upvotes: 2

Related Questions