We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

write to a file in asp.net

street216
street216 asked
on
Medium Priority
460 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
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
William,

What are you looking for help on?

Bob

Author

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
The System.IO has the File class with the Exists method:

using System.IO;

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

Bob

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
I'll give it a try. Thanks.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.