write to a file in asp.net

Posted on 2006-11-07
Medium Priority
Last Modified: 2008-02-01

I have a form that I need to save the results to a text file. The form has headers that need to be on the text file. I can append the text to the text file, but I need to make sure if the file been downloaded by the client and a new file is created, the headers are added to the top. If the headers already exist, I just need to append the text.

Or I guess the easy way would be to check if the file is empty, if it is add the headers, else append the text.

here is my code:

string FILENAME = Request.MapPath("files/comments.txt");
StreamWriter objStreamWriter;
objStreamWriter = File.AppendText(FILENAME);
objStreamWriter.WriteLine("\"" + family_name.Text.Trim() + "\",\"" + first_name.Text.Trim() + "\",\"" + DateTime.Now.ToShortDateString() + "\"");

the text file has these headers:

Question by:street216
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 96

Expert Comment

by:Bob Learned
ID: 17897516

What are you looking for help on?


Author Comment

ID: 17898184

I need to know how to check to see if the file exists and if it does not, write the headers and append the results. If it does exist, just append the results.

LVL 96

Accepted Solution

Bob Learned earned 1000 total points
ID: 17898205
The System.IO has the File class with the Exists method:

using System.IO;

if (!File.Exists(FILENAME))


Author Comment

ID: 17898221
I'll give it a try. Thanks.

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question