Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

.Net Framework 4.0: System.IO.File supports now IEnumerable<string>

.Net Framework 4.0 adds also some new and cool features to file system objects. File class has now ReadLines() methods that returns IEnumerable<string>. WriteAllLines() methods has two overload methods that accept IEnumerable<string> instead of strings array that was also supported in previous versions of .Net Framework. This posting introduces ReadLines() and WriteAllLines() methods and gives you some ideas how to use these methods in your applications.

Querying file contents

My first example shows how to use ReadLines() to query file contents using LINQ. I have text file called lorem-ipsum.txt with some paragraphs with famous lorem ipsum text. You can generate your own text on www.lipsum.com. I use LINQ query to get all lines from this file that contain word ipsum.


static void Main(string[] args)

{

    var lines = File.ReadLines("lorem-ipsum.txt");

 

    var ipsumQuery = from l in lines

                     where l.ToLower().Contains("ipsum")

                     select l;

 

    foreach (var ipsumLine in ipsumQuery)

    {

        Console.WriteLine(ipsumLine);

    }

 

    Console.ReadLine();

}


Even if you don’t use LINQ you can still consider using ReadLines() method – it doesn’t load all the contents from file to memory like ReadAllLines() does. When you are working with large files then ReadLines() is extremely useful method for you.

Writing query contents to file

My second example shows you how to write contents of IEnumerable<string> to file. ReadAllLines() method of File class has now two new overloads that accept IEnumerable<string>. To get example done with less effort we will use previous example and instead of printing lines to screen we will output them to separate file.


static void Main(string[] args)

{

    var lines = File.ReadLines("lorem-ipsum.txt");

 

    var ipsumQuery = from l in lines

                     where l.ToLower().Contains("ipsum")

                     select l;

 

    File.WriteAllLines("only-ipsum.txt", ipsumQuery);

 

    Console.ReadLine();

}


Although this example is not something markable or revolutionary it illustrates you how to query one file and output results to another file. Of course, you can use instead of file query everything else that implements IEnumerable<string> interface.

There is also AppendAllLines() method that appends lines to file instead of overwriting the file like WriteAllLines() does.

In my opinion these features are very good ones and you can use these methods in all of your applications where you need to read and query or query something and write results to files.


kick it on DotNetKicks.com pimp it 顶 Progg it Shout it
Shout it!
Posted: Oct 26 2009, 05:05 AM by DigiMortal | with 16 comment(s)
Filed under: , ,

Comments

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# October 25, 2009 10:17 PM

DotNetBurner - .net Framework said:

DotNetBurner - burning hot .NET content

# October 25, 2009 10:19 PM

PimpThisBlog.com said:

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

# October 25, 2009 10:24 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# October 25, 2009 10:26 PM

progg.ru said:

Thank you for submitting this cool story - Trackback from progg.ru

# October 25, 2009 10:29 PM

9eFish said:

9efish.感谢你的文章 - Trackback from 9eFish

# October 25, 2009 10:31 PM

Servefault.com said:

Thank you for submitting this cool story - Trackback from Servefault.com

# October 25, 2009 10:47 PM

Visual Studio 2010 and .Net Framework 4.0 - Gunnar Peipman's ASP.NET blog said:

Pingback from  Visual Studio 2010 and .Net Framework 4.0 - Gunnar Peipman's ASP.NET blog

# October 25, 2009 10:50 PM

.Net Framework 4.0: System.IO.File supports now IEnumerable<string … said:

Pingback from  .Net Framework 4.0: System.IO.File supports now IEnumerable&lt;string &#8230;

# October 26, 2009 1:19 AM

Nemesis116 said:

Very sexy :)

# October 26, 2009 3:00 AM

.Net Framework 4.0: System.IO.File supports now IEnumerable<string … Scripts Rss said:

Pingback from  .Net Framework 4.0: System.IO.File supports now IEnumerable&lt;string &#8230; Scripts Rss

# October 26, 2009 3:03 AM

Gunnar Peipman's ASP.NET blog said:

In my last posting I introduced new ReadLines() method and new overloads for WriteAllLines() method of

# October 27, 2009 5:14 AM

Nam said:

This is a nice feature to have.

# October 27, 2009 1:35 PM

Friday Links #74 | Blue Onion Software * said:

Pingback from  Friday Links #74 | Blue Onion Software *

# October 30, 2009 5:50 PM

Someone Patented Product Placement in TV Shows – Product Placement … | Advertising Marketing Wisdom said:

Pingback from  Someone Patented Product Placement in TV Shows &#8211; Product Placement &#8230; | Advertising Marketing Wisdom

# November 1, 2009 5:03 AM

A follow up to “A Short Tale of a Deceptively Slow LINQ Expression” | endjin blog said:

Pingback from  A follow up to &ldquo;A Short Tale of a Deceptively Slow LINQ Expression&rdquo; | endjin blog

# October 24, 2010 11:20 AM