Solved

write to a file in asp.net

Posted on 2006-11-07
4
428 Views
Last Modified: 2008-02-01
Hi,

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() + "\"");
objStreamWriter.Close();

the text file has these headers:
FirstName,LastName,Date

Thanks,
William
0
Comment
Question by:street216
  • 2
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17897516
William,

What are you looking for help on?

Bob
0
 

Author Comment

by:street216
ID: 17898184
Bob,

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.

Thanks,
William
0
 
LVL 96

Accepted Solution

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

using System.IO;

if (!File.Exists(FILENAME))
{
}

Bob
0
 

Author Comment

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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C#  Radio button search for Date not DateTime 4 46
Regex validation 2 24
ASP.net File format issue in producing Excel file 3 30
ASP.NET MVC -Add authentication 2 21
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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