.NET Tip: Return File Contents as a String Louisville KY

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

Developware, Inc.
1.877.280.2392
Bakery Square
Louisville, KY
Epicor Software Corporation
502-412-7666
9100 Shelbyville RD
Louisville, KY
Oasis Computer Solutions
502-429-6902
9000 Wessex Pl
Louisville, KY
Epicor Software
502-339-7748
8700 Westport RD
Louisville, KY
Information Systems Solutions Inc
502-245-7003
12730 Townepark Way
Louisville, KY
National Innovative Software Solutions LLC
502-495-0548
2108 Plantside DR
Louisville, KY
Allscripts Healthcare Solutions
502-412-1102
1302 Clear Springs Trce
Louisville, KY
Bellwether Software
502-426-5463
9900 Shelbyville Rd
Louisville, KY
Halo Group, Inc.
502-657-6468
9900 Corporate Campus Drive
Louisville, KY
Next Generation Technologies
(502) 292-4950
2351 Nelson Miller Pkwy
Louisville, KY

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

Developware, Inc.

1.877.280.2392
Bakery Square
Louisville, KY
www.developware.com

Related Local Event
SUR/FIN - Annual Technical Conference and Exhibit of Surface Finishing
Dates: 6/16/2009 - 6/18/2009
Location: Kentucky International Convention Center
Louisville, KY
View Details