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
Quote
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
try
{
//create a TextWriter then open the file
using (TextWriter writer = new StreamWriter(file))
{
//now write the message to the file
writer.Write(msg);
//date the file
writer.WriteLine("Last written to on: ");
writer.WriteLine(DateTime.Now);
}
}
catch (Exception ex)
{
//deal with any errors
Console.WriteLine(ex.Message);
}
}
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();
}
reader.Close()
//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



MultiQuote



|