Permalink
Fetching contributors…
Cannot retrieve contributors at this time
33 lines (31 sloc) 854 Bytes
// <Snippet6>
using System;
using System.Globalization;
using System.IO;
public class Example
{
public static void Main()
{
StreamReader sr = null;
try {
sr = new StreamReader("file1.txt");
String contents = sr.ReadToEnd();
sr.Close();
Console.WriteLine("The file has {0} text elements.",
new StringInfo(contents).LengthInTextElements);
}
catch (FileNotFoundException) {
Console.WriteLine("The file cannot be found.");
}
catch (IOException) {
Console.WriteLine("An I/O error has occurred.");
}
catch (OutOfMemoryException) {
Console.WriteLine("There is insufficient memory to read the file.");
}
finally {
if (sr != null) sr.Dispose();
}
}
}
// </Snippet6>