.NET Tip: Return File Contents as a String Jacksonville FL

What do you do when you need to read in an entire file and process it as a string? Here is a simple solution.

Local Companies

M A C P R O Software Inc
904-296-3900
6650 Southpoint Pkwy
Jacksonville, FL
EZ Wealth Solution
904-554-7987
1720 Detroit St
Jacksonville, FL
Gobeille Automation
904-631-8267
2421 Glen Gardner Dr
Jacksonville, FL
Advanced Technologies Solutions Inc
904-363-2223
2950 Halcyon Ln
Jacksonville, FL
Applied Engineering Management Corp
904-260-0411
7400 Baymeadows Way
Jacksonville, FL
Orion Software Group Inc
904-998-0034
9770 Old Baymeadows RD
Jacksonville, FL
Sun IT Solution
904-731-7330
7899 Baymeadows Way
Jacksonville, FL
American Medical Systems Inc
904-636-6742
7400 Baymeadows Way
Jacksonville, FL
Amtech Computer Services Inc
904-247-7752
2315 Beach Blvd Jacksonville Beach
Jacksonville, FL
Flamingo Financing
(904) 998-9077
12226 Beach Blvd
Jacksonville, FL

provided by: 
Originally published at Internet.com


The applications I've been working on lately send a lot of email notifications out as the status of the application and monitored devices change. There are a handful of different email messages that could be sent. I wanted a means to store the bodies of these emails in a manner that would be easy to update without me having to be involved. I considered storing them in the database, but that would not be very easy for the person responsible for maintaining the body of the email. Instead, I chose to store the body of each email in a file. For my application, this had the right balance of flexibility and ease of use. When I found myself starting to write the code to read in the body of an email more than, once I created a method that would read in the entire file and return it as a string. I then can easily process the body text if needed and send it out in an email. Here is the code to read a file: public static string GetFileContents(string FileName) { StreamReader sr = null; string FileContents = null; try { FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read); sr = new StreamReader(fs); FileContents = sr.ReadToEnd(); } finally { if(sr != null) sr.Close(); } return FileContents; }

This should be fairly strightforward to follow. First, a FileStream is created to control access to the file. Next, a StreamReader is created and associated with the FileStream. The ReadToEnd() method is then called to retrieve the entire contents of the file and store it in a local string variable. If the StreamReader was able to open the file successfully, it is closed and finally, the local string holding the contents of the file is returned from the method.

I added this simple method as a static method in my utility class. Now, I have easy access to reading the contents of a file from anywhere in my applications.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.

Author: Jay Miller

Read article at Internet.com site

Featured Local Company

M A C P R O Software Inc

904-296-3900
6650 Southpoint Pkwy
Jacksonville, FL
http://www.macproworld.com

Related Local Events
PWC EXCEL-erated Program
Dates: 12/8/2009 - 12/8/2009
Location: Hotel Indigo – Deerwood
Jacksonville, FL
View Details

Jacksonville Information Technology Council
Dates: 1/26/2010 - 1/26/2010
Location: Florida State College at Jacksonville- Advanced Technology Center
Jacksonville, FL
View Details

Jacksonville Information Technology Council
Dates: 11/24/2009 - 11/24/2009
Location: Florida State College at Jacksonville- Advanced Technology Center
Jacksonville, FL
View Details

Jacksonville Information Technology Council
Dates: 10/27/2009 - 10/27/2009
Location: FCCJ Advanced Technology Center
Jacksonville, FL
View Details

Jacksonville Information Technology Council
Dates: 10/27/2009 - 10/27/2009
Location: FCCJ Advanced Technology Center
Jacksonville, FL
View Details