Question

I have used DeleteFile method of the IsolatedStorageFile class to delete the file from IsolatedStorage. Sometimes this method throws IsolatedStorageException. MSDN page I referred to says that this exception is thrown when the "target file is open or the path is incorrect".

To check if the file really exists, I can call IsolatedStorageFile.FileExists method before calling DeleteFile method.

But how would I know if the file I am trying to delete is open ? I want to know it because that could be the reason why i get the exception.)


Update 1: I am trying to run multiple instances of my application.

Was it helpful?

Solution

Apparently, you are opening a file in the Isolated Storage with multiple instances of your application at the same time and you try to delete the file. Well, there's your problem. You cannot delete the file if another instance of your application has the file opened.

Here's what you can do:

  • Use a Mutex to do inter-process synchronization see MSDN and check with the other application instances if it is ok to delete the file.
  • Only allow one instance of your application to be run (although you explcitly said you wanted multiple instances, you may want to reconsider)
  • Use a seperate file for each application instance

Which solution to use depends on how your application works and what the file is used for.

EDIT: You cannot check if the file is open before deleting it since the file may be opened between checking and deleting by another application instance.

If(!FileIsOpen(path))
{
    // here, another application may open the file

    DeleteFile(path); // oops
}

What you can do is:

try
{
    DeleteFile(path);
}
catch(IsolatedStorageException)
{
    // do nothing; let the other instances try and delete the file.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top