开发者

The bizarre case of the file that both is and isn’t there

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 ha开发者_Go百科ppening would be even better.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜