0

Possible Duplicate:
Is there a way to check if a file is in use?

I saw there is a very easy way to check that a file is unlocked, no used by another process:

public static bool Check(string filepath)
{
  try
  {
    using (File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.None)) {}
  }
  catch (Exception e)
  {
    log("file not ready" + e);
    return false;
  }
  return true;
}

Well thats wonderful, but how to check if the Method would use a FileStream as parameter?

public static bool Check(FileStream f)
{ 
//...

Any suggestions? Thank you

Community
  • 1
  • 1
miri
  • 1,611
  • 5
  • 20
  • 24
  • 2
    Take a look at http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use – coolmine Sep 03 '12 at 22:48
  • @CooLMinE thats a nice thread. But they are all using a string to the full path or FileInfo. And I just have FileStream there. – miri Sep 03 '12 at 22:52
  • What you require is not possible. Except if you send a null FileStream as an argument and initialize it in the Check method. By initializing it before sending it to the Check method it means you already checked if the file can be accessed before sending it over as an argument. – coolmine Sep 03 '12 at 22:57
  • @CooLMinE Oh I thought there is somehow a way to solve this issue. So I have to rewrite this an use a string as parameter, fine. – miri Sep 03 '12 at 23:00
  • Yes, ideally you will need to send something that contains the locating of the file you want to check, string, FileInfo, anything in general that can hold the location of the file. Then just do the checking in your method and return the bool depending on the result – coolmine Sep 03 '12 at 23:02
  • But really, somehow I dont get it. If I would create a new FileStream WITH the path to the file, and send this created FileStream to my Function Check(), why the hell am I not able to get the path? :) – miri Sep 03 '12 at 23:05
  • 1
    The moment you initialize the FileStream the file will be accessed. By sending the FileStream to the method you will ultimately be checking that file twice. Once when you initialize the FileStream and once again when you do the checking in the method. – coolmine Sep 03 '12 at 23:06

1 Answers1

2

I guess it depends upon your goals, but checking this is probably not the right path for most applications I can imagine.

I am guessing you want to check this so that you can check and, if not in use, go do something (ie check then write a line of code later). The challenge here is that in the middle someone else could come along and take the file, causing you to fail in your attempted operation anyway.

Generally speaking I'd say to just try to do whatever it is you want to do and catch failure there. You need to code defensively and assume this anyway. The extra check rarely achieves much.

Eric Fleischman
  • 1,168
  • 6
  • 8
  • 1
    Thats right. If the file is ready I would do something with it. I could lock the file if Check() => true ;) That would solve the problems. But Im still dont know how to do this only with FileStream – miri Sep 03 '12 at 22:55
  • 2
    Don't check at all. Try to do whatever it is you really want to do. If that fails, look at the errors and handle it in that code path. (IE look for failure due to someone else having a handle on the file when you try to do the real operation) – Eric Fleischman Sep 03 '12 at 22:57