UnauthorizedAccessException "访问路径被拒绝";来自 LOCALAPPDATA 中的 File.ReadAllBytes [英] UnauthorizedAccessException "Access to the path is denied" from File.ReadAllBytes in LOCALAPPDATA

查看:19
本文介绍了UnauthorizedAccessException "访问路径被拒绝";来自 LOCALAPPDATA 中的 File.ReadAllBytes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在读取%LOCALAPPDATA%中的文件时,同一用户在同一台​​机器上间歇性地发生此异常.

This exception is occurring intermittently for the same user for the same machine, when reading files within %LOCALAPPDATA%.

研究

我已经检查了此标题当前提供的所有可能的重复项(有很多).有一个与阅读有关一个没有答案的 AES 加密文件;我认为不适用,因为这些文件没有加密.

I have checked all the possible duplicates currently offered by this title (there are a lot). There is one relating to reading an AES encrypted file which has no answer; and I don't believe applies, since these files are not encrypted.

其中大部分与写入文件有关(但我正在读取文件),或者是 MSDN 上为 File.ReadAllBytes(string).

Most of them are to do with writing files (but I'm reading a file), or are the obvious causes as documented on MSDN for File.ReadAllBytes(string).

此异常的三种解释是:

  1. 当前平台不支持此操作"——不知道是什么意思;但考虑到这有时适用于同一台机器上的同一用户(我将在下面解释),我想我可以排除这种情况.
  2. "path 指定了一个目录" - 正如你从下面的代码中看到的,调用是在检查 File.Exists 时进行的,所以我想我可以排除这种情况.
  3. 呼叫者没有所需的权限."这是对此异常的通常解释,我怀疑我遇到了某种边缘案例".
  1. "This operation is not supported on the current platform" - I don't know what that means; but given that this works sometimes for the same user on the same machine (I'll explain below), I think I can rule this out.
  2. "path specified a directory" - as you'll see from the code below, the call is made within a check of File.Exists, so I think I can rule this out.
  3. "The caller does not have the required permission." This is the usual explanation for this exception, and I suspect I'm getting some kind of "fringe case" of this.

场景

当以域用户身份运行的应用程序正在读取同一用户的 %LOCALAPPDATA% 子文件夹中的文件时会发生这种情况(该用户应该没有权限问题)读取文件).其中的子文件夹只是遵循正常的公司名称"\应用程序名称"结构,并且没有对子文件夹应用额外的权限(我们只是使用该文件夹来使我们的文件远离其他人).

This is occurring when an application which is running as a domain user is reading a file inside a subfolder of %LOCALAPPDATA% of the same user (for which there should be no permission issues for that user to read files). The subfolders within that just follow the normal "CompanyName"\"ApplicationName" structure, and there are no additional permissions applied on the sub folders (we're just using the folder to keep our files away from other people's).

例外

System.UnauthorizedAccessException:对路径[redacted]"的访问是否认.在 System.IO.__Error.WinIOError(Int32 errorCode, StringmayFullPath) 在 System.IO.FileStream.Init(String path, FileMode模式、FileAccess 访问、Int32 权限、Boolean useRights、FileShare共享,Int32 bufferSize,FileOptions 选项,SECURITY_ATTRIBUTESsecAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath,Boolean checkHost) 在 System.IO.FileStream..ctor(String path,FileMode 模式,FileAccess 访问,FileShare 共享,Int32 bufferSize,FileOptions 选项、字符串 msgPath、布尔值 bFromProxy、布尔值useLongPath, Boolean checkHost) 在System.IO.File.InternalReadAllBytes(String path, Boolean checkHost)
下面的代码

System.UnauthorizedAccessException: Access to the path '[redacted]' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.File.InternalReadAllBytes(String path, Boolean checkHost)
at the code below

代码

        // Note that filename is within %LOCALAPPDATA%
        if (File.Exists(fileName))
        {
            var readAllBytes = File.ReadAllBytes(fileName); // exception here
            // etc...
        }

证明是间歇性的

并且我可以从我们的错误日志和其他信息的组合中证明这在大多数时间有效,并且我可以证明机器和特定组合的以下事件序列用户:

And I can prove from the combination of our error logs, and other information that this is working most of the time, and I can prove the following sequence of events for a particular combination of machine and user:

  • 应用程序正常运行,然后
  • 此异常发生(可能多次,每次重试延迟呈指数增加:1 分钟、2 分钟、4 分钟等),然后
  • 应用程序再次运行

我认为文件系统不会发生任何实质性更改(例如权限)来修复它.我想知道这是否可能是由边缘权限问题引起的,例如,他们的密码是否即将过期,或者最近是否已更改.

I don't believe that any material change (e.g. permissions) will have occurred to the file system in order to fix it. I'm wondering whether this might be caused by a fringe permissions issue, for example, if their password is about to expire, or has recently been changed.

当我注意到这个错误发生时,我有一个具体的例子,我建议用户重新启动他们的机器,问题就消失了.

I have a specific example when I noticed this error happening, and I advised the user to reboot their machine, and the problem went away.

问题

谁能给我一个权威的解释,超出我已经猜到的,或者确认它是什么?

Can anyone give me an authoritative explanation of the cause of this, above what I've already guessed, or to confirm what it is?

推荐答案

这个问题太宽泛了,但我想指出,除了你列出的之外,还有其他原因导致拒绝访问异常.例如,考虑这个简单的程序:

The question is too broad, but I want to point out that there are other reasons for access denied exception besides you listed. For example, consider this simple program:

public class Program {
    static string _target = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "test", "test.txt");
    static void Main(string[] args) {
        File.Create(_target).Dispose();
        ProcessFile();

        // below throws access denied
        if (File.Exists(_target))
            Console.WriteLine(File.ReadAllText(_target));
        Console.ReadKey();
    }

    static void ProcessFile() {
        // open and abandon handle
        var fs = new FileStream(_target, FileMode.Open, FileAccess.Read, FileShare.Delete);
        // delete
        File.Delete(_target);
    }        
}  

这里我们在 %LOCALAPPDATA% 下创建新文件,并使用 FileShare.Delete 打开它,但不关闭.FileShare.Delete 允许对文件进行后续删除,但在关闭文件的所有句柄之前不会实际删除文件.

Here we create new file under %LOCALAPPDATA%, and open it with FileShare.Delete, but not closing. FileShare.Delete allows subsequent deletion of the file, but file will not be actually deleted until all handles to it are closed.

然后我们继续File.Delete,它实际上并没有删除文件,而是将其标记为删除,因为我们仍然有打开的文件句柄.

Then we proceed with File.Delete, which does not actually delete file but marks it for deletion, because we still have open file handle to it.

现在,File.Exists 为此类文件返回 true,但尝试访问它会抛出您描述的拒绝访问"异常.

Now, File.Exists returns true for such file, but trying to access it throws "Access denied" exception as you described.

很难判断这种特定情况是否与您的案例相关,但可能是.

Whether this specific situation is relevant to your case is hard to tell, but it might be.

我的观点主要是:您应该期待此类异常(以及文件已在使用中"类型的异常)并通过重试来处理它们.它们可能因您无法控制的各种原因而发生.

My point mainly is: you should expect such exceptions (and also "file already in use" kind of exceptions) and handle them by retrying. They can happen for various reasons outside of your control.

这篇关于UnauthorizedAccessException "访问路径被拒绝";来自 LOCALAPPDATA 中的 File.ReadAllBytes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆