0

I am using File.ReadAllLines to read the contents of a text file. But, if the file has the ReadOnly property, or if the user does not have Write permission to the file, then the ReadAllLines method throws an exception saying "Access to path 'path-to-file' is denied".

Is there some way to use File.ReadAllLines and read the contents of a ReadOnly file?

ekad
  • 14,436
  • 26
  • 44
  • 46
Peter
  • 1,292
  • 4
  • 15
  • 33

2 Answers2

1

I apologise - I was misled by the MSDN documentation, which says that the UnauthorizedAccessException is thrown when the path argument passed to the method specifies a file that is read-only.

In fact, you CAN issue a ReadAllLines for a read-only file, in spite of the MSDN documentation.

Peter
  • 1,292
  • 4
  • 15
  • 33
-1

Why not try to read with StremReader:

var content = new StreamReader("FilePath").ReadToEnd();
Liam
  • 27,717
  • 28
  • 128
  • 190
  • this will have the exact same problem because the file is read only and you are not specifying the FileAccess flag. – Liam Feb 03 '14 at 11:51
  • 1
    This error is not related to ReadOnly, is related to who have permissions to open the folder or file. If the user that tries to open don't have access you get this error. If the file is ReadOnly doesn't matter to Read. If you change the file to another folder with full permissions, you will open it or if you give permissions to the folder with the current user. – Miguel Angel Utiel Peñaranda Feb 03 '14 at 12:28