我正在使用.NET Compact Framework的3.5和想要删除某些特定文件夹及其子文件夹。当我运行应用程序,它给 IO异常
。我试着使用 Directory.Delete(路径)
方法,但没有奏效。
我该如何解决这个问题呢?
下面是我的code:
使用系统;
使用System.Diagnostics程序;
使用System.IO;
使用System.Linq的;
使用System.Collections.Generic;
使用System.Windows.Forms的;
命名空间Reset_Client
{
静态类节目
{
静态无效的主要(){
MYFUNC();
的MessageBox.show(Cihaz resetlendi!);
}
公共静态无效MYFUNC()
{
字符串主路径= @\存储卡\ deneme;
尝试
{
DeleteDirectory(MainPath的+CRM);
DeleteDirectory(MainPath的+BHTS);
DeleteDirectory(主路径+图像);
DeleteDirectory(主路径+风);
DeleteDirectory(主路径+表);
DeleteDirectory(主路径+日志);
File.Delete(MainPath的+Agentry.ini);
File.Delete(MainPath的+Agentry.app);
File.Delete(MainPath的+Agentry.usr);
}
赶上(IOException异常E)
{
MYFUNC();
}
}
公共静态无效DeleteDirectory(字符串TARGET_DIR)
{
FileInfo的的fileInfo =新的FileInfo(TARGET_DIR);
FileAttributes属性= fileInfo.Attributes;
如果((属性和放大器; FileAttributes.ReadOnly)== FileAttributes.ReadOnly)
{
//设置属性nonreadonly
fileInfo.Attributes和放大器; =〜FileAttributes.ReadOnly;
}
字符串[]文件= Directory.GetFiles(TARGET_DIR);
字符串[]迪尔斯= Directory.GetDirectories(TARGET_DIR);
的foreach(在文件中字符串的文件)
{
File.Delete(文件);
}
的foreach(在显示目录字符串DIR)
{
DeleteDirectory(DIR);
}
Directory.Delete(TARGET_DIR,假);
}
}
}
解决方案
为什么不删除目录递归:
Directory.Delete(路径,真实);
请参阅这里。
此外,请参阅here因为它可能是类似于您遇到。
I'm working with the .NET Compact Framework 3.5 and want to delete some specific folders and their subfolders. When I run the app it gives IO exception
. I've tried to use Directory.Delete(path)
method but it didn't work.
How can I solve this problem?
Here is my code :
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Reset_Client
{
static class Program
{
static void Main(){
myfunc();
MessageBox.Show("Cihaz resetlendi!");
}
public static void myfunc()
{
string mainPath = @"\Storage Card\deneme";
try
{
DeleteDirectory(mainPath + "CRM");
DeleteDirectory(mainPath + "BHTS");
DeleteDirectory(mainPath + "IMAGES");
DeleteDirectory(mainPath + "STYLES");
DeleteDirectory(mainPath + "TABLES");
DeleteDirectory(mainPath + "LOG");
File.Delete(mainPath + "Agentry.ini");
File.Delete(mainPath + "Agentry.app");
File.Delete(mainPath + "Agentry.usr");
}
catch (IOException e)
{
myfunc();
}
}
public static void DeleteDirectory(string target_dir)
{
FileInfo fileInfo = new FileInfo(target_dir);
FileAttributes attributes = fileInfo.Attributes;
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
// set the attributes to nonreadonly
fileInfo.Attributes &= ~FileAttributes.ReadOnly;
}
string[] files = Directory.GetFiles(target_dir);
string[] dirs = Directory.GetDirectories(target_dir);
foreach (string file in files)
{
File.Delete(file);
}
foreach (string dir in dirs)
{
DeleteDirectory(dir);
}
Directory.Delete(target_dir, false);
}
}
}
解决方案
Why not delete the directory recursively:
Directory.Delete(path, true);
See here.
Also, see here as it may be similar to what you are encountering.