ellapt

T12.3.ReadTextFile

Jan 25th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Security;
  4.  
  5. class ReadTextFile
  6. {
  7. static void Main()
  8. {
  9. Console.WriteLine("Enter file name with its full path, read and print it on the console");
  10. Console.Write("Enter full path&filename: ");
  11. string fileName = Console.ReadLine();
  12. try
  13. {
  14. ReadFile(fileName);
  15. }
  16. catch (FileNotFoundException)
  17. {
  18. Console.WriteLine("The file '{0}' was not found!", fileName);
  19. }
  20. catch (DirectoryNotFoundException)
  21. {
  22. Console.WriteLine("Directory not found!");
  23. }
  24. catch (SecurityException)
  25. {
  26. Console.WriteLine("You don't have the required permission to access this file!");
  27. }
  28. catch (UnauthorizedAccessException ae)
  29. {
  30. Console.WriteLine(ae.Message);
  31. }
  32. catch (ArgumentException)
  33. {
  34. Console.WriteLine("File name not correct!");
  35. }
  36. catch (IOException ioe)
  37. {
  38. Console.WriteLine(ioe.Message);
  39. }
  40. catch (NotSupportedException)
  41. {
  42. Console.WriteLine("Invalid path format!");
  43. }
  44. }
  45.  
  46. static void ReadFile(string fName)
  47. {
  48. string fileText = File.ReadAllText(fName);
  49. Console.WriteLine("The content of the file is: ");
  50. Console.WriteLine(fileText);
  51.  
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment