Mark
Mark

Reputation: 6977

How can I detect if a StorageFile was renamed or deleted in a UWP app?

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:

Upvotes: 1

Views: 658

Answers (2)

Gilad Noy
Gilad Noy

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

Sebastian
Sebastian

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

Related Questions