Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

Why not try to read with StremReader:

var content = new StreamReader("FilePath").ReadToEnd();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top