0
votes

I am trying to delete the folder created in isolated storage. But i get a error of "path must be a valid file name" My created file name is "a07292011//time.Schedule".

So now i want to delete the folder and my code is:

myStore.DeleteDirectory(selectedFolderName1 + "\\");

Where selectedFolderName1 = a07292011

3
I hope, no need to append (+ "\\") with directory name. Because your going to delete folder itself.Naga Harish M

3 Answers

5
votes

Here's my code to recursively delete foldersand theirfiles/subfolders from the isolated storage. It works on Windows Phone 8, too.

public static void CleanAndDeleteDirectoryRecursive(string directory)
    {
        IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
        if (iso.DirectoryExists(directory))
        {
            string[] files = iso.GetFileNames(directory + @"/*");
            foreach (string file in files)
            {
                iso.DeleteFile(directory + @"/" + file);
                Debug.WriteLine("Deleted file: " + directory + @"/" + file);
            }

            string[] subDirectories = iso.GetDirectoryNames(directory + @"/*");
            foreach (string subDirectory in subDirectories)
            {
                CleanAndDeleteDirectoryRecursive(directory + @"/" + subDirectory);
            }

            iso.DeleteDirectory(directory);
            Debug.WriteLine("Deleted directory: " + directory);
        }
    }
4
votes

The directory you try to delete must be empty.

public void DeleteDirectory(string directoryName) {
try {
    using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication()) {
        if (!string.IsNullOrEmpty(directoryName) && currentIsolatedStorage.DirectoryExists(directoryName)) {
            var fn = isoFile.GetFileNames(string.Concat(directoryName, "\\*"));
            if (fn.Length > 0)
                for (int i = 0; i < fn.Length; ++i)
                    isoFile.DeleteFile(string.Concat(directoryName, "\\", fn[i]));
            isoFile.DeleteDirectory(directoryName);
        }
    }
} catch (Exception ex) {
    //implement some error handling
}
}
3
votes
    /// <summary>
    /// Method for deleting an isolated storage directory
    /// </summary>
    /// <param name="directoryName">Name of a directory to be deleted</param>
    public static void DeleteDirectory(string directoryName)
    {
        try
        {
            using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!string.IsNullOrEmpty(directoryName) && currentIsolatedStorage.DirectoryExists(directoryName))
                {
                    currentIsolatedStorage.DeleteDirectory(directoryName);
                }
            }
        }
        catch (Exception ex)
        {
            // do something with exception
        }
    }

Get more details here

http://www.eugenedotnet.com/2010/11/isolated-storage-for-windows-phone-7/