Adding text to a tab delimited file with a foreach

I am creating a tab delimited file and I am trying to use a foreach loop to write data in a row - I was wondering if anyone knew how to do that?
if (sfdToPC.ShowDialog() == DialogResult.OK)
            {
                myPath = Path.GetFileName(sfdToPC.FileName);
                FileStream fs = new FileStream(myPath, FileMode.Create, FileAccess.ReadWrite);
                StreamWriter outputFile = new StreamWriter(fs);
                string thedate = jobSummary[0].CreatedOn.ToString("dd MMM yyyy ");
                string theTime = jobSummary[0].CreatedOn.ToString("hh:mm:ss");
                string thefile = "Surefire Accepted Survey Data as Text" + "\r\n\r\n" +
                                 "Job File Name = " + wellname + ".itj" + "\r\n" +
                                 "Job Notes: " + jobSummary[0].JobNotes.ToString() + "\r\n" +
                                 rig + "\r\n" +
                                 rig + " " + rignumber + "\r\n" +
                                 "PV" + "\r\n" +
                                 "Created On = " + thedate + " @ " + theTime + "\r\n" +
                                 totalSurveys + " Total Surveys in File. " + totalAcceptedSurveys + " are tagged as Accepted" + "\r\n" +
                                 "\r\n" +
                                 "The Vertical Depth and Maximum Departure values below were calculated using the Minimum Curvature Model." + "\r\n" +
                                 "This information is intended for reference purposes ONLY." + "\r\n" +
                                 "It is NOT intended to replace professional oversight or sound judgement." + "\r\n" +
                                 "For more detail on the data and algorithms used to compute this information, contact your sales agent." + "\r\n" +
                                 "\r\n\r\n" +
                                 "SURVEY DEPTH" + "\t" + "VERTICAL DEPTH" + "\t" + "MAXIMUM DEPARTURE" + "\t" + "TGF" + "\t" + "BATTERY" + "\t" + "TEMPERATURE" + "\t" + "SURVEY #" + "\t" + "SURVEY TYPE" + "\t" + "SURVEY DATE AND TIME" + "\t" + "BIT DEPTH" + "BIT TRIP NUMBER" + "\t" + "BIT TO TOOL OFFSET" + "\r\n";
                                 foreach(var list in MyAcceptedSurveyList)
                                 {
                                     list.SurveyDepth.ToString() += "\t" + list.SurveyAngle.ToString() += "\t";  

                                 }

Open in new window

LVL 6
r3nderAsked:
Who is Participating?
 
AndyAinscowFreelance programmer / ConsultantCommented:
So, what is your problem?
0
 
r3nderAuthor Commented:
ok I added a string builder  but I need to add that data that is in the for loop to the existing file
if (sfdToPC.ShowDialog() == DialogResult.OK)
            {
                StringBuilder sb = new StringBuilder();
                myPath = Path.GetFileName(sfdToPC.FileName);
                FileStream fs = new FileStream(myPath, FileMode.Create, FileAccess.ReadWrite);
                StreamWriter outputFile = new StreamWriter(fs);
                string thedate = jobSummary[0].CreatedOn.ToString("dd MMM yyyy ");
                string theTime = jobSummary[0].CreatedOn.ToString("hh:mm:ss");
                string thefile = "Surefire Accepted Survey Data as Text" + "\r\n\r\n" +
                                 "Job File Name = " + wellname + ".itj" + "\r\n" +
                                 "Job Notes: " + jobSummary[0].JobNotes.ToString() + "\r\n" +
                                 rig + "\r\n" +
                                 rig + " " + rignumber + "\r\n" +
                                 "PV" + "\r\n" +
                                 "Created On = " + thedate + " @ " + theTime + "\r\n" +
                                 totalSurveys + " Total Surveys in File. " + totalAcceptedSurveys + " are tagged as Accepted" + "\r\n" +
                                 "\r\n" +
                                 "The Vertical Depth and Maximum Departure values below were calculated using the Minimum Curvature Model." + "\r\n" +
                                 "This information is intended for reference purposes ONLY." + "\r\n" +
                                 "It is NOT intended to replace professional oversight or sound judgement." + "\r\n" +
                                 "For more detail on the data and algorithms used to compute this information, contact your sales agent." + "\r\n" +
                                 "\r\n\r\n" +
                                 "SURVEY DEPTH" + "\t" + "VERTICAL DEPTH" + "\t" + "MAXIMUM DEPARTURE" + "\t" + "TGF" + "\t" + "BATTERY" + "\t" + "TEMPERATURE" + "\t" + "SURVEY #" + "\t" + "SURVEY TYPE" + "\t" + "SURVEY DATE AND TIME" + "\t" + "BIT DEPTH" + "BIT TRIP NUMBER" + "\t" + "BIT TO TOOL OFFSET" + "\r\n";
                outputFile.WriteLine(thefile);
                outputFile.Close();
                foreach (var list in MyAcceptedSurveyList)
                {
                    sb.Append(list.SurveyDepth.ToString() + "\t" + list.SurveyAngle.ToString() + "\t" + "\r\n");

                }

                using (StreamWriter outfile = new StreamWriter(myPath))
                {
                    outfile.WriteLine(sb.ToString());
                }

                //outputFile.WriteLine(thefile);
                outputFile.Close();

Open in new window

0
 
r3nderAuthor Commented:
found out how to  do it added true to the new streamwriter so it appended to the file thanks Andy
 foreach (var list in MyAcceptedSurveyList)
                {
                    sb.Append(list.SurveyDepth.ToString() + "\t" + list.SurveyAngle.ToString() + "\t" + "\r\n");

                }

                using (StreamWriter outfile = new StreamWriter(myPath,true))
                {
                    outfile.WriteLine(sb.ToString());
                }

Open in new window

0
 
r3nderAuthor Commented:
Thank for telling me I was on the right path
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.