Solved

write to a file in asp.net

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Rolling back a sequence of stored procedures 12 38
How to LOAD up a .NET Framework website with data? 2 40
Cannot upload files above 1mb IIS7 11 39
asp.net mvc return json 2 54
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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.…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now