質問

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?

役に立ちましたか?

解決

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.

他のヒント

Why not try to read with StremReader:

var content = new StreamReader("FilePath").ReadToEnd();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top