0

Possible Duplicate:
How to test if a file is fully copied in .NET

As of almost 2012, is there still no better way to check if a file is fully copied as to open it within a try~catch block?

I know it's been asked before and I've checked the automatic related questions Stack Overflow offers, but they are over two years old, so perhaps a better solution exists today?

This is a similar question of 2009: How to test if a file is fully copied in .NET

EDIT: I changed the question's title since it raised "requestioning" issues.

Community
  • 1
  • 1
Alejandro B.
  • 4,807
  • 2
  • 33
  • 61
  • @AlejoBrz, repeating a question that has already been asked in order to draw new attention to it is not the way SO works. That is what bounties are for (though admitedly you do not have the rep for it). – Kirk Woll Dec 14 '11 at 18:03
  • exactly, as I said on my question and even added a link to it :-Þ but I dud mentioned that it was posted two years ago... – Alejandro B. Dec 14 '11 at 18:04
  • @Kirk, I was not trying to draw attention to it but to see if a better solution came up, since catching an error seems like a really dirty way to do this. – Alejandro B. Dec 14 '11 at 18:05
  • @KirkWoll Seems like the real question is not actually a duplicate but would be better worded as "How to test if a file is fully copied with .Net without using a try-catch". – Derrick H Dec 14 '11 at 18:05
  • @Derrick, still a dup. It's all just minor variations of [this question](http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use). – Kirk Woll Dec 14 '11 at 18:06
  • 1
    @AlejoBrz -- that's precisely what I mean. One of the pre-defined "bounty reasons" is to seek a more contemporary answer because the old one is out of date. – Kirk Woll Dec 14 '11 at 18:07

1 Answers1

3

No, there still isn't a way to do that without using try ... catch, and if I were you I wouldn't hold my breath waiting for one.

What you're really asking for is a group of functions that can tell you whether or not an attempt to open a file in a particular mode will succeed. Something like a File.CanOpenForExclusiveRead() or something similar. There are no such methods in .NET, and they don't exist in the Windows API. And for good reason.

Let's say that such a function existed. That is, you could write:

if (File.CanOpenForExclusiveRead(filename))
{
    f = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.None);
}

Sounds like a good idea, right? Except you'd still need to put a try ... catch around the FileStream constructor because some other thread (perhaps in another process) might open the file for exclusive read between the time your code checked and the time your code called the constructor. Or something else could go wrong: the file could be deleted, the disk could go offline, etc.

To be sure, that fictional File.CanOpenForExclusiveRead method would be useful in some cases. Those cases, though, are few and far between, and as I showed above they wouldn't do you a whole lot of good anyway, since you'd still have to handle possible exceptions anyway.

If you want a method that does that, write your own that uses try ... catch and returns false if an exception is thrown. At least then you hide the details of the implementation. But don't wait for a better solution. It's highly unlikely that one is forthcoming.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351