holgerm
holgerm

Reputation: 949

C# File.ReadAllText() throws "wrong" exception when dir path given - why?

C# method File.ReadAllText(string filePath) does not accept a directory path as argument - it needs a path to a file. I understand that.

But why does it in this case throw an UnauthorizedAccessException instead of an ArgumentException? I think this is not intuitive.

Has anybody an explanation that makes me feel better about this?

Upvotes: 2

Views: 1374

Answers (2)

SX10
SX10

Reputation: 40

This happens when your final exe file needs some privilleges.

Try to run as an admin.

Upvotes: 0

Ipsit Gaur
Ipsit Gaur

Reputation: 2927

File.ReadAllText throws UnauthorizedAccessException in the following cases:

path specified a file that is read-only.
-or-
This operation is not supported on the current platform.
-or-
path specified a directory.
-or-
The caller does not have the required permission.

Refer to the documentation for File.ReadAllText for more info.

Upvotes: 6

Related Questions