In C#, files are treated simply as streams. If a text file is written in lines, each line ends with an end-of-line marker (a CR/LF pair).
There are lots of methods and properies involved; this is just a brief look at the subject.
Suppose you want to read from a file in C#. Then:
Example:
Suppose we have a text file called "fruits.txt" containing a few lines:
apple banana melon fig
We can read these and echo each string to the screen:
using System; using System.IO; namespace ConsoleApplication5 { class Program { static void Main() { using (StreamReader SR = new StreamReader("fruits.txt")) { String S; S = SR.ReadLine(); while (S != null) { Console.WriteLine(S); S = SR.ReadLine(); Console.ReadLine(); } } } } }
When this is run, the user needs to hit the enter key after each line (because of ReadLine()). We could leave that out if we wanted.
Another way to detect the end of the file is to use the EndOfStream property of the StreamReader class, which is true if the current position is at the end of the stream and false otherwise.
This example does not handle any exceptions. It probably should. For instance, if the file does not exist, we will have an exception. We could avoid this by using the Exists method of the File class, as in:
if (File.Exists("filename"))
We could also have a path to the file. You may not need a fully-qualified path and name, but it must be possible to find the file, starting from whatever directory contains the executable file. One approach is to do the following:
String Path = Directory.GetCurrentDirectory(); using (StreamReader SR = new StreamReader(path + "\\fruits.txt") { ... }
In the Visual Studio IDE, your project files are in some directory. The path you need is the path to that directory + "/bin/debug". (We usually run our programs with debugging.) Put the input file there, as that is where the executable file will exist.
An open file is an example of an "unmanaged" resource, and it will not automatically be closed and the StreamReader object will not be deleted simply because we reach the end of the scope. The StreamReader class has a Dispose() method to do this, implementing the IDisposable interface. (Various other unmanaged resources do the same.)
The effect of the "using" statement in Main() is to guarantee that the file will be closed and the resource (StreamReader) will be properly deleted afterward. To be more precise, the Dispose() method will be called automatically at the end of the scope of "using".
Suppose you want to write to a text file in C#. Then:
Example:
using (StreamWriter SW = new StreamWriter("TestFile.txt")) { SW.WriteLine("This is an example."); }
As we are using WriteLine, there will be an end-of-line marker, a CR/LF pair.
Again, a StreamWriter object is unmanaged, and we need the "using" block.
The File class was mentioned above. It is a static class which has methods for file operations such as opening, closing, reading, writing, copying, etc. Here are a couple of its methods:
public static bool Exists(string PathFileName)
This returns true if the string identifies an existing file and the user has permission to read it, and false otherwise.
public static FileAttributes GetAttributes(string PathFileName)
This uses the FileAttributes enumeration, which contains a number of values each reflecting one bit of information: is the file compressed, is it hidden, is it read-only, is it a directory, etc.
Here is an example from a MSDN page:
FileAttributes attributes = File.GetAttributes("c:/Temp/testfile.txt"); if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { Console.WriteLine("read-only file"); } else { Console.WriteLine("not read-only file"); }