Reputation: 6977
For a Windows Store app: how can I detect if a StorageFile
was renamed or deleted outside of my application while it is open in my app?
I have a Windows 10 UWP app running on the desktop. The app lets the user open and edit documents.
Things I've tried:
storageFile.GetBasicPropertiesAsync().DateModified
, but even it I delete the file and empty the trash, the call returns successfully with the (old) DateModified
. (I assume it uses an in-memory version and does not check the file on disk)StorageFile.GetFileFromPathAsync(file.Path)
. This correctly threw a FileNotFoundException
the first time. Unauthorized access/permission denied
exception. It kind of makes sense, because I need the user to pick the file in a FileOpenPicker to have my app get permission to use it.StorageFolder.CreateFileQuery()
, but I can't access the parent folder form the StorageFile instance (again, it makes sense, because my app does not have permission to access the parent folder)Upvotes: 1
Views: 658
Reputation: 91
Found a nice and short way to check if file was deleted:
public async bool StorageFileExists(StorageFile file)
{
try
{
var stream = await StorageFile.OpenReadAsync();
stream.Dispose();
return true;
}
catch (FileNotFoundException e)
{
return false;
} //Other exceptions are not caught on purpose and should propagate
}
Upvotes: 1
Reputation: 413
Even in a UWP App you can use System.IO.File.Exists(String)
.
https://msdn.microsoft.com/de-de/library/system.io.file.exists(v=vs.110).aspx
Upvotes: 0