Wednesday, April 6, 2011

Delete directory in IsolatedStorage

IsolatedStorageFile.DeleteDirectory works with empty directories only.
I wrote the method for recursive deleting content of the directory:

using System.IO.IsolatedStorage;
. . .
        public void DeleteDirectory(IsolatedStorageFile fs, String dirName)
        {
            String pattern = dirName + @"\*";
            String[] files = fs.GetFileNames(pattern);
            foreach (String fName in files)
            {
                fs.DeleteFile(Path.Combine(dirName, fName));
            }
            String[] dirs = fs.GetDirectoryNames(pattern);
            foreach (String dName in dirs)
            {
                DeleteDirectory(fs, Path.Combine(dirName, dName));
            }
            fs.DeleteDirectory(dirName);
        }

Usage:
            using (IsolatedStorageFile fs = IsolatedStorageFile.GetUserStoreForApplication())
            {
                DeleteDirectory(fs, @"\Projects"); // directory "Projects" in the root will be deleted
            }


No comments:

Post a Comment