开发者

fast way to delete files

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开发者_如何学JAVA(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


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...


Dont forget to check is a file is in use.

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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜