Lowest Hosting Price on The Planet

Recent Articles

Return Format For ColdFusion Components...
One of the lesser publicized updates in ColdFusion 8 is the introduction of the returnFormat attribute for the cffunction tag. What does this do? ColdFusion Components, when executed remotely, will return...

Getting The Value Of AJAX-ified Controls
I ran into a problem last night trying to use JavaScript to read the value of a rich text field. I had assumed I could use the normal syntax I'd use for a form field: document.forms[0].body.value or document...

Enhancements In ArcGIS At 9.3 & Simple Mashup...
ArcGIS Server 9.3, the next version of the popular app will be an incremental release… support for integrated security, better cashing, more options in building apps and simple mashup applications, deploy REST...

Adobe's ColdFusion 8 Beta
The wait is over and the latest version of ColdFusion has just been released in beta. Code named 'Scorpio' (the eighth sign of the Zodiac) during the development stages, the new release is officially named...

Section 508 & WAI Triple A Accessibility
I've been fiddling with accessibility standards and validation today. It's something I've been doing for several years now on many different types of web applications. So today it was time for my blog...


07.20.07


.NET - Beware Of The FileInfo.OpenText() Method

By Mads Kristensen

I'm a big fan of the System.IO.FileInfo object in .NET because it wraps the System.IO.File object nicely in a strongly typed way.

It makes it easier to work with files. The FileInfo class has a method called OpenText that returns a StreamReader instance which can then be read into a string and other things.

If you use the OpenText method read the text of a file, then the easiest way is like so:



Now the content variable contains the text content of the currency.xml file, but there is a problem with this approach.

Because the OpenText method creates a StreamReader instance which we then call the ReadToEnd method on, the StreamReader keeps a lock on the file.

This can cause many problems and must be avoided.

Lowest Hosting Price on The Planet

Instead you could do like so, which releases the file handle when the StreamReader is disposed:



This works fine, but we doubled the lines needed to read the text.

This might not be an issue, but then we could just as well just use the StreamReader directly without using the FileInfo class like so:



It is much cleaner than using the FileInfo class and the intent is very clear as well.

Comments


About the Author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

About TheDevWeb
TheDevWeb examines the best tools and techniques to help you keep up with all the latest trends in the fast paced world of web development. Excellence in Development Starts Here

TheDevWeb is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
DatabaseProNews.com SQLProNews.com
ITcertificationNews.com SysAdminNews.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITmanagementNews.com



-- TheDevWeb is an iEntry, Inc. publication --
iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509
2007 iEntry, Inc. All Rights Reserved Privacy Policy Legal

archives | advertising info | news headlines | free newsletters | comments/feedback | submit article

Excellence in Development Starts Here TheDevWeb News Archives About Us Feedback TheDevWeb Home Page About Article Archive News Downloads WebProWorld Forums Jayde iEntry Advertise Contact