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);