简体   繁体   English

C# File.ReadAllText() 在给定目录路径时抛出“错误”异常 - 为什么?

[英]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. C# 方法File.ReadAllText(string filePath)不接受目录路径作为参数 - 它需要文件路径。 I understand that.我明白。

But why does it in this case throw an UnauthorizedAccessException instead of an ArgumentException?但是为什么在这种情况下它会抛出 UnauthorizedAccessException 而不是 ArgumentException 呢? I think this is not intuitive.我认为这不直观。

Has anybody an explanation that makes me feel better about this?有没有人的解释让我对此感觉更好?

File.ReadAllText throws UnauthorizedAccessException in the following cases: File.ReadAllText在以下情况下抛出UnauthorizedAccessException

path specified a file that is read-only. path指定了一个只读文件。
-or- -或-
This operation is not supported on the current platform.当前平台不支持此操作。
-or- -或-
path specified a directory. path指定了一个目录。
-or- -或-
The caller does not have the required permission.调用者没有所需的权限。

Refer to the documentation for File.ReadAllText for more info.有关详细信息,请参阅File.ReadAllText文档

This happens when your final exe file needs some privilleges.当您的最终 exe 文件需要一些特权时,就会发生这种情况。

Try to run as an admin.尝试以管理员身份运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM