Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

Why not try to read with StremReader:

var content = new StreamReader("FilePath").ReadToEnd();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top