最终System.UnauthorizedAccessException的:访问路径...否认路径、System、UnauthorizedAccessException

2023-09-07 04:57:22 作者:心丶不再完整

我有一个简单的C#控制台程序,要求如下功能:

I have a simple C# console-program which calls the function shown below:

static void DirTest()
{
    string dir = "Temp";
    for (int i = 0; i < int.MaxValue; i++)
    {
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }
        string file = Path.Combine(dir, "sample.txt");
        File.Create(file).Close();
        File.Delete(file);
        Directory.Delete(dir);
    }
}

在一些Win 7的机器,该功能最终会抛出异常(当我超过约10万):

On some Win 7 machines this function eventually throws exception (when i is more than some 100,000):


        Unhandled Exception: System.UnauthorizedAccessException: Access to the path 'D:\... \Temp\sample.txt' 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_ATTRIBU
    TES secAttrs, String msgPath, Boolean bFromProxy)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare sh
    are, Int32 bufferSize, FileOptions options)
       at System.IO.File.Create(String path)
       at Exceptions.Program.DirTest() in D:\Exceptions\Program.cs:line 118
       at Exceptions.Program.Main(String[] args) in D:\Exceptions\Program.cs:
    line 167

这些机器具有迈克菲代理和思科安全代理和/ W安装在主机其他s的。 Windows Defender的禁用。该程序运行在管理员控制台。程序编译为.NET 3.5。这个我没有在W2K3或XP的计算机看到的。

These machines have McAfee agent and Cisco security agent and a host of other s/w installed. Windows defender is disabled. The program is running in a Administrator console. The program is compiled for .net 3.5. This I did not see on W2k3 or XP machines.

如果我使用procmon中监视所有事件和进程正在访问其中TEMP \ sample.txt的创建和删除的文件夹,我看到,除了测试应用程序中没有其他进程正在访问的路径。即使在异常后有在procmon中列出的任何其他进程。因此,我不能证明它是抗病毒惹的祸。

If I use procmon to monitor all events and processes that are accessing the folder where "Temp\sample.txt" is created and deleted, I see that except for the test-application no other processes are accessing the path. Even after the exception there are no other processes listed in procmon. So I cannot prove that it is the fault of anti-virus.

没有任何人有任何想法可能是错误的? 难道我赶上一个错误,在.NET上的Win7;)

Does anybody have any ideas what might be wrong ? Did I catch a bug in .net on Win7 ;)

谢谢!

推荐答案

@whywhywhy

@whywhywhy

我修改了code稍微看看我是否能得到一些更多的信息,如果它会出错误在我结束。

I modified your code slightly to see if I could get a little more info, if it would error out on my end.

static void DirTest()
{
    string dir = "Temp";
    int i = 0;
    try
    {
        for (i = 0; i < int.MaxValue; i++)
        {
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            string file = Path.Combine(dir, "sample.txt");
            File.Create(file).Close();
            File.Delete(file);
            Directory.Delete(dir);
            System.Console.WriteLine("Finished i: " + i);
        }
    }
    catch
    {
        System.Console.WriteLine("Error on i: " + i);
        throw;
    }
}

我通过200000次迭代就去了,没有问题。我甚至注释掉的WriteLine的循环,这将使其更快地执行,仍然没有问题。

I went through 200,000 iterations without issues. I even commented out the writeline in the loop, which would make it perform faster, still no issues.

我不知道哪个版本使用Visual Studio的,但是你可能想尝试一些事情: 1)检查的VS和Windows更新。 2)检查驱动程序的更新您的硬盘驱动器。

I'm not sure which version of visual studio you are using, however you might want to try a couple things: 1) Check for updates for VS and windows. 2) Check for driver updates for your hard drive.

或许你可以运行修改后的code与print语句太...,看它是否是一个时间问题。 print语句可能足以让事情顺利进行。如果是这样的话,那么我的猜测是一样的衙门。

Perhaps you can run the modified code with the print statement too... to see if it is a timing issue. The print statement might be enough to allow things to run smoothly. If this is the case, then my guess would be same as yamen's.