Solved

write to a file in asp.net

Posted on 2006-11-07
4
429 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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