New Features in .NET Framework 4.0-Part03

No.of Views1790
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  08 Jul 2010 02:07:55
Tag : CSharp , CSharp4.0
In the .NET Framework 4.0 have lots of new features for software professionals. Today I would like to share I/O operation in File class
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In the .NET Framework 4.0 have lots of new features for software professionals. Today I would like to share I/O operation in File class.

Method No 1:File.ReadLines

This is new method in .NET Framework 4.0 with File class. This method accepts the path of the file to read and encoding. Then it will return the collection of IEnumerable<string>.

Method Definition

//// Summary://     Reads the lines of a file.//// Parameters://   path://     The file to read.//// Returns://     The lines of the file.//// Exceptions:// Few no of exception has applicable[SecuritySafeCritical]public static IEnumerable<string> ReadLines(string path);

 CodeSnippet

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace ConsoleApplication1
{class Program{static void Main(string[] args){// New Features in for IO -ReadLinesvar lines = File.ReadLines("File.txt");foreach (string item in lines){Console.WriteLine(item);}}}}

 Method No 2:File.WriteAllLines

This is old method, but it accepts the new parameter to write all lines from source content. When we are calling this method in .NET Framework 4.0, we will able to pass IEnumerable <string> as one of the argument.

CodeSnippet

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace ConsoleApplication1
{class Program{static void Main(string[] args){// New Features in for IO -ReadLinesvar lines = File.ReadLines("File.txt");// New Features in IO-WriteAllLines, with  IEnumerable File.WriteAllLines("desti.txt", lines);}}}

 Method No 3: File.AppendAllLines

This is another new method in .NET Framework under the File class. To this method can pass the destination file path and IEnumerable <string> collection.

Method Definition

// Summary://     Appends lines to a file, and then closes the file.//// Parameters://   path://     The file to append the lines to. The file is created if it does not already//     exist.////   contents://     The lines to append to the file.//// Exceptions:// Few no of exception has applicable[SecuritySafeCritical]public static void AppendAllLines(string path, IEnumerable<string> contents);

Code Snippet

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace ConsoleApplication1
{class Program{static void Main(string[] args){// New Features in for IO -ReadLinesvar lines = File.ReadLines("File.txt");// Just appened read lines in to the file with IEnumerableFile.AppendAllLines("sasds.txt", lines);}}}

 That’s all and I like these features, because reduce no of lines code to perform I/O operation, enjoy the new features in .NET Framework 4.0. I will post more new features in future as well.

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
    Since we have multicore processors are now so we can take advantages of multicore processor with parallel execution in C# 4.0. There are some time consuming task for the computer for example a long for loop or similar kind of things. This kind of task can be done parallel with parallel class in C# 4.0.
    Published Date : 12/Jul/2011
    In this article, i will explain how to use the How to use ExpandoObject Class in .NET 4.0.The ExpandoObject class is introduced on .NET Framework 4.0 and inherited using many interfaces.
    Published Date : 16/Apr/2011
    C# 4.0 supports Dynamic Programming by introducing new Dynamic Typed Objects
    Published Date : 26/May/2010
    Dynamic Datatype in C# 4.0 is quit bit familiar to Var Datatype. Deference between Var and Dynamic is VAR initialized on CompileTime and Dynamic initialized On Runtime
    Published Date : 17/May/2010
    In this article I will show how to use the IObserver and IObservable Interfaces in C#. These two interfaces is work with connection to Push based approach on Reactive Framework
    Published Date : 15/Dec/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</