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!