Reputation: 13496
I get the following exception when I try to delete a directory in Isolated Storage in Windows Phone 7:
An error occurred while accessing IsolatedStorage.
there is no inner exception.
using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{
isf.DeleteDirectory(dir.TrimEnd('/'));
}
Notes:
Any idea?
Thanks.
Upvotes: 1
Views: 2610
Reputation: 31
`public static void DeleteDirectoryRecursive(string directory, IsolatedStorageFile store)
{
if (!store.DirectoryExists(directory))
return;
var pattern = Path.Combine(directory, "*");
foreach (var file in store.GetFileNames(pattern))
{
store.DeleteFile(Path.Combine(directory, file));
}
foreach (var folder in store.GetDirectoryNames(pattern))
{
DeleteDirectoryRecursive(Path.Combine(directory, folder), store);
}
store.DeleteDirectory(directory);
}`
Upvotes: 1
Reputation: 4896
Grabbed Valipour's version and make it work. Added some checks to improve stability + fixed some names. This works for me on Lumia 920.
private void DeleteDirectoryRecursive(string dir)
{
if (String.IsNullOrEmpty(dir)) return;
try
{
using (var isoFiles = IsolatedStorageFile.GetUserStoreForApplication())
{
foreach (var file in isoFiles.GetFileNames(dir + "\\*"))
{
var filename = dir + "/" + file;
if (isoFiles.FileExists(filename))
isoFiles.DeleteFile(filename);
}
foreach (var subdir in isoFiles.GetDirectoryNames(dir))
{
var dirname = dir + subdir + "\\";
if (isoFiles.DirectoryExists(dirname))
DeleteDirectoryRecursive(dirname);
}
var currentDirname = dir.TrimEnd('\\');
if (isoFiles.DirectoryExists(currentDirname))
isoFiles.DeleteDirectory(currentDirname);
}
}
catch (Exception e)
{
throw;
}
}
Upvotes: 0
Reputation: 48095
Thanks to valipour, I solved the problem
foreach (var file in isf.GetFileNames(dir))
{
isf.DeleteFile(dir + file);
}
In my case the variable dir is "images". In order to get all file names in "images" directory, you should use isf.GetFileNames("images/*")
Upvotes: 0
Reputation: 13496
Ok, problem solved, problem was that files were not being deleted correctly. The reason I was confused is that IsolatedStorageFile class does not warn you when you are deleting an invalid file. here is the correct code and some notes:
public static void DeleteDirectoryRecursive(this IsolatedStorageFile isf, string dir)
{
foreach (var file in isf.GetFileNames(dir))
{
isf.DeleteFile(dir + file);
}
foreach (var subdir in isf.GetDirectoryNames(dir))
{
isf.DeleteDirectoryRecursive(dir + subdir + "\\");
}
isf.DeleteDirectory(dir.TrimEnd('\\'));
}
Notes:
Upvotes: 6
Reputation: 26341
According to your code and your description, you would be recreating the IsolatedStorageFile access on every iteration?
You should post all the code, since the error isn't related to what you told so far. As for a working example, see this blog post. If that fails with your directory name, you're clearly doing something wrong.
Also, I believe it uses backslashes, not forward-slashes for paths, so your Trim() would be rather useless either way.
Upvotes: 1