我正在尝试删除在独立存储中创建的文件夹。 但我得到错误“路径必须是有效的文件名” 我创建的文件名为“a07292011 // time.Schedule”。
所以现在我要删除文件夹,我的代码是:
myStore.DeleteDirectory(selectedFolderName1 +“\\”);
selectedFolderName1 = a07292011
答案 0 :(得分:5)
这是我的代码,用于从隔离存储中递归删除文件夹及其文件/子文件夹。它也适用于Windows Phone 8。
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);
}
}
答案 1 :(得分:4)
您尝试删除的目录必须为空。
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
}
}
答案 2 :(得分:3)
/// <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
}
}
在此处获取更多详情
http://www.eugenedotnet.com/2010/11/isolated-storage-for-windows-phone-7/