tags:

views:

249

answers:

5

Hello,

i want to delete some files but only if they aren't in use. What i did was a try/catch:

    Try
        My.Computer.FileSystem.DeleteFile(fileInfo.FullName)
    Catch ex As Exception

    End Try

but it seems that this method is very slow if i try to delete some files over network.

Is there an faster way to delete files? Is it faster to check first if an file is open? If yes, how can i check if an file is open?

Kind regards Nico

+1  A: 

If the problem is a slow network connection, all you can do about it is get a faster network. Does the problem happen only across the network?

If possible, try running your code on the machine that's hosting the files, so that you won't need to go through the network.

SLaks
+1  A: 

One of the fundamental problems with the idea is of course that it's inherently a race condition. A file that's in use |now| may not be in use |now|, and vice versa. It could still be a benefit for performance reasons. However, I do suspect that you're trying to delete multiple files, despite your coude showing the deletion of only one. It probably makes sense to use a threadpool for this, as you can try multiple deletions in parallel.

[edit]

I also notice that you use My.Computer.FileSystem.DeleteFile(String). This has a small problem; it needs to lookup the file by name again. That's not the cheapest operation on a network. SHFileOperation might be more effective, but it is a complex native API function.

MSalters
If the underlying problem is network bandwidth, the ThreadPool might not help.
SLaks
But it might if the problem is latency.
Joren
@MSalters: are you sure about the name lookup? I don’t think `FileInfo` can skip name lookup when invoking `Delete`. A `FileInfo` doesn’t have to correspond to an existing file, not even to an existing directory. Granted, the situation may be different for network shares but I don’t think so. Furthermore, the `FileInfo` instances must first be constructed and even if you get them by iterating over a directory they are probably constructed using the normal constructor.
Konrad Rudolph
Can't say in general for all .Net implementations. I know that for my (native) class, the equivalent of FileInfo held a lot of optional data. For instance, it could hold an entire `WIN32_FIND_DATA`. I didn't know how to use `WIN32_FIND_DATA::reserved0` but I can imagine .Net using it for its FileInfo class.
MSalters
+1  A: 

You might be better to use the more specific exception called IOException instead. As for deleting files from the network, there would obviously be a latency as a result, bear in mind that files on the network would be opened by others. You could check the LastAccessTime/LastAccessWrite property of the fileinfo class to see if it is current, then you would know that it is in use.

Hope this helps, Best regards, Tom.

tommieb75
Access time is just a hint you may miss some files that would have been deleted.
Guillaume
+2  A: 

Tips : If you are deleting a directory, you should use Directory.Delete(String, Boolean).

If you work with FileInfo, call FileInfo.Delete() it may or may not be faster. Your issue seems to be in your network...

Guillaume
It's probably faster; it can skip the name lookup
MSalters
+1  A: 

Dont forget to check is a file is in use.

http://msdn.microsoft.com/en-us/library/system.io.stream.canwrite.aspx

Roger