views:

84

answers:

3

In .Net 3.5, I have the following code.

If File.Exists(sFilePath & IndexFileName & ".NX") Then
  Kill(sFilePath & IndexFileName & ".NX")
End If

At runtime, on one client's machine, I get the following exception, over and over, when this code executes

Source: Microsoft.VisualBasic
TargetSite: Microsoft.VisualBasic.FileSystem.Kill
Message: No files found matching 'I:\RPG\HGIAPVXD.NX'.
StackTrace: 
   at Microsoft.VisualBasic.FileSystem.Kill(String PathName)
(More trace that identifies the exact line of code.)

There are two people on different machines running this code, but only one of them is getting the exception. The exception does not happen every time, but it is happening regularly. (Multiple times every hour.) The code is not in a loop, nor does it run continuously, more like once every couple of minutes or so.

On the surface, this looks like a race condition, but given how infrequently this code is run and how often the error is happening I think there must be something else going on.

I would appreciate any suggestions on how I can track down what is really going on here. A solution to keep the error from happening would be even better.

+5  A: 

I guess the first question to ask is "IS the file really there or not?" and if so, does it have any specical attributes (Is it Read-only or Hidden, or System --- or a Directory)?

Note the Microsoft.VisualBasic.FileSystem.Kill specifically looks for, and silently skips, any file marked "System" or "Hidden". For pretty much any other problem you would have gotten a different exception.

James Curran
Probably it would be better to use `File.Delete` here.
0xA3
We can't singlestep through the code on the client machine, but watching the directory indicates that the file is there prior to the kill, and afterwards is gone, even when it throws an exception. i.e. It looks like the kill works, then throws an exception. It acts like a race condition, I just find it hard to believe that this particular code result in the a race condition this frequently.
RB Davidson
Also, I took your advice and changed the code to use File.Delete. We haven't gotten the change to the client yet to see if that solves the problem.
RB Davidson
@RB: This is getting weird. FileSystem.Kill expect a wildcard path like 'I:\RPG\*.NX'. You get that error only if there are no files matching that pattern. Only if it finds something will it try to delete it. Basically, it sound like you're calling FileSystem.Kill twice.
James Curran
Wierd is right. This code runs on many machines, but the error happens on one, and only when two people are entering data at the same time. I'm wondering if it isn't caused by a lag in how fast the server marks the file as deleted in NTFS. I don't know how NTFS works internally, but here is my suspicion. Kill on computer A deletes the file on the drive, then queues up the change for NTFS. Computer B does File.Exists before NTFS is updated, check passes. When B does Kill, it sees file is actually deleted and throws an exception. Its a stretch, but its the best guess I've got at the moment.
RB Davidson
A: 

as James pointed out the Kill functions checks if the file in case is a system or hidden, you better use System.IO.File.Delete() instead

    Try
        System.IO.File.Delete(sFilePath & IndexFileName & ".NX")
    Catch ex As System.Exception
        ...
    End Try

using File.Exits is not neccasary because File.Delete() checks this by itself.

marc.d
A: 

Is there any chance that the I: drive is a network drive? it could be some network issue... or then maybe a race condition

Uoli
Yes, the I drive is on a network file share. Race condition seems like the most probable cause, except that the timing on the two computers involved would have to me amazingly precise to reproduce this error over and over.
RB Davidson