/// <summary>
/// 删除指定文件、文件夹
/// </summary>
/// <param name="dir">目录路径</param>
/// <param name="delname">待删除文件或文件夹名称</param>
public void DeleteFolder(string dir,string delname)
{
if (Directory.Exists(dir))
{
foreach (string d in Directory.GetFileSystemEntries(dir))
{
string tmpd = d.Substring(0, d.LastIndexOf("\\")) + "\\" + delname;
if (Directory.Exists(d))
{
if (d == tmpd)
Directory.Delete(d, true);
else
DeleteFolder(d, delname);//递归删除子文件夹
}
else if(File.Exists(d))
{
if (d == tmpd)
File.Delete(d);
}
}
}
}
/// <summary>
/// 以下为遍历文件目录文件名,并保存到数组al
/// </summary>
public ArrayList GetAllDirList(string strBaseDir)//strBaseDir是路径
{
public ArrayList al = new ArrayList();//定义存储文件和文件夹名的数组(该数组用于查找指定目录的完整目录)
DirectoryInfo di = new DirectoryInfo(strBaseDir);
DirectoryInfo[] diA = di.GetDirectories();
if (aaa == 0)
{
FileInfo[] fis2 = di.GetFiles(); //有关目录下的文件
for (int i2 = 0; i2 < fis2.Length; i2++)
{
al.Add(fis2[i2].FullName);
//fis2[i2].FullName是根目录中文件的绝对地址,把它记录在ArrayList中
}
}
for (int i = 0; i < diA.Length; i++)
{
aaa++;
//al.Add(diA[i].FullName + "\t<目录>");
//diA[i].FullName是某个子目录的绝对地址,把它记录在ArrayList中
DirectoryInfo di1 = new DirectoryInfo(diA[i].FullName);
DirectoryInfo[] diA1 = di1.GetDirectories();
FileInfo[] fis1 = di1.GetFiles(); //有关目录下的文件
for (int ii = 0; ii < fis1.Length; ii++)
{
al.Add(fis1[ii].FullName);
//fis1[ii].FullName是某个子目录中文件的绝对地址,把它记录在ArrayList中
}
GetAllDirList(diA[i].FullName);
//注意:递归
}
return al;
}