1

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);
    }
}

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

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.

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.

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

Thanks!

whywhywhy
  • 278
  • 1
  • 5
  • 15
  • Why you are doing anything like this? – ABH Apr 12 '12 at 07:38
  • This could very well be a problem with the underlying disk drivers or NTFS itself, and nothing to do with .NET. Creating and deleting a directory and file over 100,000 times as fast as you can is unlikely to be healthy for the OS, the disk or your program. As @hamad says, and your username begs it also, whywhywhy? – yamen Apr 12 '12 at 07:45
  • My money is on User Account Control (UAC) being the issue here. Have you tried running your prog elevated to admin privileges? – Bridge Apr 12 '12 at 07:51
  • May be you are looking for this: – sarwar026 Apr 12 '12 at 08:11
  • hamad, yamen - The reason I wrote this test is that I work on a s/w that does something similar 24/7 though not on the same folder, but on huge file shares. – whywhywhy Apr 12 '12 at 08:53
  • sarwar026 - The other question you show is failure each time. In my case this is failure after 100,000 successes. – whywhywhy Apr 12 '12 at 08:55
  • Bridge - as I mentioned in the other comment it succeeds 100,000 times before failing. If it is a UAC issue it should happen each time. The program is already running in a "Run As Admin" console. – whywhywhy Apr 12 '12 at 08:57

2 Answers2

1

Code works at my Maschine Windows 7 / 64 Bit

I guess that your "Anti Virus" Program is the Problem which checkes every created File.

I got McAffee runing but no Cisco..

Hope this answer could be of any help ;)

sebastianmehler
  • 1,033
  • 1
  • 11
  • 23
  • 1
    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. – whywhywhy Apr 12 '12 at 09:10
1

@whywhywhy

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;
    }
}

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.

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.

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.

James Oravec
  • 19,579
  • 27
  • 94
  • 160