我正在尝试删除在独立存储中创建的文件夹。 但我得到的错误“路径必须是有效的文件名” 我创建的文件名是“a07292011 // time.Schedule”。在windows phone 7中删除独立的存储目录
所以现在我要删除的文件夹,我的代码是:
myStore.DeleteDirectory(selectedFolderName1 + “\\”);
其中selectedFolderName1 = a07292011
我正在尝试删除在独立存储中创建的文件夹。 但我得到的错误“路径必须是有效的文件名” 我创建的文件名是“a07292011 // time.Schedule”。在windows phone 7中删除独立的存储目录
所以现在我要删除的文件夹,我的代码是:
myStore.DeleteDirectory(selectedFolderName1 + “\\”);
其中selectedFolderName1 = a07292011
/// <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/
试过了。我有访问隔离存储的错误 –
是否currentIsolatedStorage.DirectoryExists(directoryName)返回true? –
http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.deletedirectory.aspx检查该URL。 **目录在被删除之前必须是空的。删除的目录无法恢复,一旦删除。** –
试图删除必须为空目录。
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
}
}
这里是我的代码递归删除文件夹和他们的文件/子文件夹从孤立的存储。它也适用于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);
}
}
我希望,不需要追加(+“\\”)目录名称。因为你要删除文件夹本身。 –