-1

I am playing a *.mp4 file in media player but at the same time trying to move the file from one place to another I need to check if the file is being accessed by another process i want to block the move process.

public Boolean fileInUse(FileInfo file)
{
    FileStream stream=null;
    try
    {
        stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);

    }
    catch (IOException)
    {
        //File is in use
        Console.WriteLine("File is Being used");
        return true;
    }
    finally {
        if (stream != null) {
            stream.Close();
        }
    }
    Console.WriteLine("File is not in use");
    return false;    
}
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 3
    Does this answer your question? [Is there a way to check if a file is in use?](https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use) – Natrium Dec 24 '19 at 05:10
  • Keep in mind that even if you write code that says "if the file's not in use, then move it", another app could sneak in and open the file between the time you do your "if" and the time you do your "then". The only guaranteed way to make sure you can move the file is to actually move it, and catch any exceptions. Of course you can still do a preemptive check like you're asking about, e.g. to give a better user experience (e.g. disabling the "move" button if you know you can't move the file). But when you finally do the move, you still have to be prepared to catch exceptions. – Joe White Dec 24 '19 at 05:51
  • @JoeWhite I don't have a UI for the move process particularly it is supposed to be automated cannot disable the button cause I don't have one . – Karanrajsinh Jadeja Dec 24 '19 at 06:02
  • @JoeWhite There won’t necessarily be an exception as you can move files that are in use. – ckuri Dec 24 '19 at 10:10

1 Answers1

0
public Boolean fileInUse(FileInfo file)
{
    bool fileInUse = false;
    FileStream stream = null;
    try
    {
        stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
        stream.Close();
    }
    catch (IOException)
    {
        //File is in use
        Console.WriteLine("File is Being used");
        fileInUse = true;
    }

    Console.WriteLine("File is not in use");
    return fileInUse;    
}
dino
  • 58
  • 6
  • The problem remains the same even if the file is in use it never throws an IOException – Karanrajsinh Jadeja Dec 26 '19 at 03:30
  • There is no such function for a file in general. It really depends upon what you are trying to do. If, for example, you want to open a file just once then you'll be using a stream. The existence of the stream instance (meaning not null) is generally indicative that it is open because of how you will scope it. If you want to know, in general, whether file X is open by anybody then there is no such support. – dino Dec 26 '19 at 08:44
  • It would be almost useless anyway as by the time the call returns somebody could have opened or closed the file. Most people try to detect an open file before they try to write to it but that is the incorrect approach. A file can be opened for reading and writing by different people at the same time. In general you'll just try the operation and if it fails, and you're interested, then you can check the exception to see why it failed. – dino Dec 26 '19 at 08:44