UnauthorizedAccessException in Vista from frequent file I / O with .Net 2.0

At work, we switched from Windows XP to Windows Vista. After the migration, some of my unit tests, using nUnit, began to randomly fail when calling System.UnauthorizedAccessException. Each failed test involves writing files used for tests stored as embedded resources in the test DLL to the current directory, running tests, and then deleting them, usually when setting up / breaking or setting up / decoupling devices quickly. I do this so that my tests are incompatible with the location on each drive of the developer from which they are run, and not worry about the relative file paths.

Eliminating this, I found that it was related to creating and deleting files. When deleting, each delete follows the pattern:

if( File.Exists(path) ) { File.Delete(path) } 

When I surround this with a try-catch block and a catch breakpoint (if an exception was thrown), the file will already be deleted from disk. For crashes in creating files, usually using XmlWriter or StreamWriter, each of them is specified to overwrite the file if it exists.

The fuzzy thing, when researching, I created this program in C #, which seems to recreate the exception:

 class Program { static void Main(string[] args) { int i = 0; try { while (true) { System.IO.TextWriter writer = new System.IO.StreamWriter("file.txt"); i++; System.Console.Out.WriteLine(i); writer.Write(i); writer.Close(); System.IO.File.Delete("file.txt"); } } catch (System.UnauthorizedAccessException ex) { System.Console.Out.WriteLine("Boom at: " + i.ToString()); } } } 

On one of our machines, which still has XP, she will continue to sort through hundreds of thousands, without exception, until I kill her. On any of our Vista machines, it will print “Boom” anywhere between 150 and 500 iterations.

Since I do not have access to the Vista machine outside of work, I cannot determine if this particular “quirk” is due to my employer’s security configuration of Vista or Vista itself.

Suffice it to say that I am quite puzzled.

EDIT:

I would like to thank everyone for their answers. I used the Process Monitor suggested by Christian, and found that the Windows SearchIndexer and TortoiseSVN TSVNCache processes were trying to access the target file while my code was running, as Martin suggested.

Thanks again.

+4
source share
3 answers

Do you have antivirus scanners? Try disabling them or watching file activity using ProcMon from http://www.sysinternals.com

0
source

In Vista, go to Performace Monitor (controlpanrl-> Administrative tools) and observe the virtual bytes (for memory leaks) for your application when it is running. The performance monitor gives you a lot of details about the process you want to research. I suspect your application is not freeing resources, as it works very hard with the file system.

Also play with the performance monitor to try and see other measures that might cause your problem. It’s hard to blame one or the other service without doing a little investigation.

+1
source

Could this randomly run into a background service, thus indexing the file? Try disabling as many services as you can.

+1
source

All Articles