Deleting isolated storage directories in windows phone 7

怎甘沉沦 提交于 2019-12-17 21:24:52

问题


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


回答1:


    /// <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/




回答2:


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);
        }
    }



回答3:


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
}
}


来源:https://stackoverflow.com/questions/6858050/deleting-isolated-storage-directories-in-windows-phone-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!