-
Notifications
You must be signed in to change notification settings - Fork 4
/
ReadFile.cs
42 lines (40 loc) · 1.18 KB
/
ReadFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* Write a program that enters file name along with its full file path (e.g. C:\WINDOWS\win.ini),
* reads its contents and prints it on the console. Find in MSDN how to use System.IO.File.ReadAllText(…).
* Be sure to catch all possible exceptions and print user-friendly error messages.
*/
using System;
using System.IO;
class ReadFile
{
static void Main(string[] args)
{
try
{
Console.Write("Enter directory and file to be read: ");
string path = @Console.ReadLine();
StreamReader reader = new StreamReader(@path);
using (reader)
{
string file = reader.ReadToEnd();
Console.WriteLine(file);
}
}
catch (FileLoadException fle)
{
Console.Error.WriteLine(fle.Message);
}
catch (FileNotFoundException fnfe)
{
Console.Error.WriteLine(fnfe.Message);
}
catch (AccessViolationException ave)
{
Console.Error.WriteLine(ave.Message);
}
catch (UnauthorizedAccessException uae)
{
Console.Error.WriteLine(uae.Message);
}
}
}