I am trying to delete a folder from Isolated Storage which has files and folders inside recursively. I am using a piece of code suggested by others on Stackoverflow and other blogs. The code is as follows:
private void deleteSubApp(string pappname)
{
try
{
string directory = "apps/" + pappname;
IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
if (iso.DirectoryExists(directory))
{
string[] files = iso.GetFileNames(directory + @"/*");
foreach (string file in files)
{
try
{
iso.DeleteFile(directory + @"/" + file);
}
}
string[] subDirectories = iso.GetDirectoryNames(directory + @"/*");
foreach (string subDirectory in subDirectories)
{
try
{
deleteSubApp(directory + @"/" + subDirectory);
}
}
iso.DeleteDirectory(directory);
}
}
}
Since Windows Phone 8 does not allow a built in function to delete a folder unless its empty, deleting it recursively as the above code remains the only option. But when i run the code, i get an exception which is:
System.IO.IsolatedStorage.IsolatedStorageException: Unable to delete, directory not empty or does not exist.
Please help to find any errors in the code, due to which it is failing? Or am I missing something?
Change
deleteSubApp(directory + @"/" + subDirectory);
To
deleteSubApp(pappname + @"/" + subDirectory);
Otherwise you pass in something like "apps/pappname/subdirectory" on the recursive call and it will set directory
to "apps/apps/pappname/subdirectory" which does not exist.