Page 1 of 1

System.IO in C# Part I

#1 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Post icon  Posted 03 September 2007 - 04:23 PM

In Part I of this tutorial we will be looking at dealing with text files and delimited files in C#. The three main classes for working with text files are:All of these are a member of the System.IO Namespace, which contains objects and classes for reading and writing to data streams, and that provide file and directory functionality.

In this tutorial we will start with basic file and directory operations such as reading a file, writing to a file, and checking if a file exists before working with. In part II we will look at more advanced items such as creating a directory, converting a delimited file to an XML document, reading certain lines from a file, and others.

The first example we will have it writing a value to a text file. In this example we use the using statement. According to MSDN.Com, this is what they say about the using statement


C#, through the .NET Framework common language runtime (CLR), automatically releases the memory used to store objects that are no longer required. The release of memory is non-deterministic; memory is released whenever the CLR decides to perform garbage collection. However, it is usually best to release limited resources such as file handles and network connections as quickly as possible, the Using statement allows this to happen without hainv to explicitly closing the object or connection.

Now lets look at writing data to a text file

/// <summary>
/// Method for writing to a file
/// </summary>
/// <param name="file">File to write to</param>
/// <param name="msg">Text to write to the file</param>
public void WriteToFile(string file,string msg)
	//always use a try...catch to deal 
	//with any exceptions that may occur
		//create a TextWriter then open the file
		using (TextWriter writer = new StreamWriter(file))
		   //now write the message to the file
		   //date the file
		  writer.WriteLine("Last written to on: ");
	catch (Exception ex)
		//deal with any errors

Here we create an instance of the StreamReader class to open the file. We then use the Write Method to write our text to the file. The variable msg is the text passed to the method that is then weitten to the file. In the above method we are introduced to 2 of the TextWriter Members:The main difference between the two is WriteLine adds a line terminator to the end of the line.The next thing we will look at is reading from a text file. In this function we will read an entire file into a string variable a line at a time, then return the value to the calling method. For this we are introduced to the TextReader Class.

Lets take a look at how we would do this

public static string ReadFileLineAtATime(string file)
	  /   /create a string variable to hold the file contents
		 string fileContents = string.Empty;
		 //create a new TextReader then open the file
		 TextReader reader = new StreamReader(file)
		  //loop through the entire file
		  while (reader.Peek() != -1)
				//add each line to the fileContents variable
				fileContents += reader.ReadLine().ToString();
		  //return the results
		  return fileContents;

Here we created a string variable fileContents to hold the contents of the file, then we loop through the file a line at a time adding it to our variable. We are then introduced to two fo the TextReader MembersPeek is used to determine when we are at the end of the file, as long as it does not return -1, there is still more to read. Inside the loop we read the file a line at a time appending each line to our variable.

There are three more Members of the TextReader Class other than the ReadLine mentioned above
Continue to Part 1.5 of this tutorial

Is This A Good Question/Topic? 2
  • +

Replies To: System.IO in C# Part I

#2 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Posted 15 June 2009 - 07:30 PM

Just a comment,

There is an error with Pyschocoder's definition of Peek.

Peek returns a -1 only if no more characters can be read or the stream does not support seeking. (The code is correct, but the explanation may confuse people)

This post has been edited by eker676: 15 June 2009 - 07:31 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1