0
private void DisplayLastTakenPhoto()
{
    string mypath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),"RemotePhoto");
    var directory = new DirectoryInfo(mypath);
    var myFile = directory.EnumerateFiles()
        .Where(f => f.Extension.Equals(".jpg", StringComparison.CurrentCultureIgnoreCase) || f.Extension.Equals("raw", StringComparison.CurrentCultureIgnoreCase))
        .OrderByDescending(f => f.LastWriteTime)
        .First();

    LiveViewPicBox.Load(myFile.FullName);
}

protected virtual bool IsFileLocked(FileInfo file)
{
    FileStream stream = null;

    try
    {
        stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
    }
    catch (IOException)
    {
        //the file is unavailable because it is:
        //still being written to
        //or being processed by another thread
        //or does not exist (has already been processed)
        return true;
    }
    finally
    {
        if (stream != null)
            stream.Close();
    }

    //file is not locked
    return false;
}

The problem is with the line:

LiveViewPicBox.Load(myFile.FullName);

Sometimes it's working fine sometimes i'm getting exception on this line say the file is in use by another process.

So i want to use the IsFileLocked method or some other method to check untill the file is not locked. But if i will call this method before the line

LiveViewPicBox.Load(myFile.FullName);

It will check if the file locked only once. I need somehow to use while or somet other way to check if the file is locked over and over again until it's unlocked. And only when it's unlocked to make the line LiveViewPicBox.Load(myFile.FullName);

  • You might love to read about [MSDN: FileSystemWatcher](https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx) it listens to the file system change notifications and raises events when a directory, or file in a directory, changes. – Mohit S Dec 15 '15 at 02:01

1 Answers1

-2
public static bool IsFileReady(String sFilename)
{
    // If the file can be opened for exclusive access it means that the file
    // is no longer locked by another process.
    try
    {
        using (FileStream inputStream = File.Open(sFilename, FileMode.Open, FileAccess.Read, FileShare.None))
        {
            if (inputStream.Length > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
    }
    catch (Exception)
    {
        return false;
    }
}

Place this in a loop and wait for it to return true.

  • This is not a real answer. It can return true and one millisecond later be locked again. – Camilo Terevinto Dec 15 '15 at 02:08
  • @cFrozenDeath He specifically asked for a way to check for a file being unlocked consistently, he did not ask for a way to lock a file. – user1598725 Dec 15 '15 at 02:10
  • It's not professional and nice to CopyPaste answers with nothing to add. And Clearly should have been marked duplicate for what i commented or from the place you took the answer: http://stackoverflow.com/questions/50744/wait-until-file-is-unlocked-in-net – Orel Eraki Dec 15 '15 at 02:11
  • @CollinM.Stevens what makes you think there's some consistency here? I didn't say "to lock a file", I said that the file might be blocked again after the method returns true. – Camilo Terevinto Dec 15 '15 at 02:15
  • @OrelEraki I can not mark for duplicate, nor comment on answers or else I would have. I answered his question, nothing more, nothing less. I was not going to flag his question for moderator intervention as that is the ONLY option I have available for flagging. – user1598725 Dec 15 '15 at 02:15
  • @cFrozenDeath **It will check if the file locked only once. I need somehow to use while or somet other way to check if the file is locked over and over again until it's unlocked. And only when it's unlocked to make the line LiveViewPicBox.Load(myFile.FullName);** I answered that question in my answer. I understand that the file could be blocked after the method returns; that wasn't his question. – user1598725 Dec 15 '15 at 02:17
  • If you can't mark it as Duplicate, than report to Moderator or let the other community handle this issue, but adding **Fire** (encouraging the user not to look in Google for a solution as a minimal requirement) will not help out community. – Orel Eraki Dec 15 '15 at 02:19