using System;
using System.Collections.Generic;
using System.Text;
using System.IO;//
using System.Collections;//
namespace ControlFile
{
/// <summary>
/// 文件操作类
/// </summary>
public class ControlFileClass
{
//字段声明
private ArrayList fileListPath = new ArrayList();
private ArrayList fileListName = new ArrayList();
/// <summary>
/// 文件路径
/// </summary>
public ArrayList FileListPath
{
get { return fileListPath; }
}
/// <summary>
/// 文件名称
/// </summary>
public ArrayList FileListName
{
get { return fileListName; }
}
/// <summary>
/// 构造函数并遍历文件夹获取文件名称,路径
/// </summary>
/// <param name="sourceDirectory">文件夹路径</param>
/// <param name="nextFold">是否继续查找更深路径</param>
public ControlFileClass(string sourceDirectory, bool nextFold)
{
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
GetAllList(diSource,nextFold);
}
public void GetAllList(DirectoryInfo source, bool nextFold)
{
try
{
foreach (FileInfo fi in source.GetFiles())
{
fileListPath.Add(fi.FullName);
fileListName.Add(fi.Name);
}
if (nextFold)//如果设置为可以向更深目录遍历则遍历
{
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
GetAllList(diSourceSubDir, nextFold);
}
}
else//如果设置为不向更深目录遍历则直接用文件夹表示
{
//遍历获取文件夹
foreach (DirectoryInfo d in source.GetDirectories())
{
fileListPath.Add(d.FullName);
fileListName.Add(d.Name);
}
}
}
catch
{ }
}
/// <summary>
/// 判断一个路径是文件还是文件夹
/// </summary>
/// <param name="fileName">文件路径</param>
/// <returns>true:文件夹,false:文件</returns>
public static bool IsFolder(string fileNamePath)
{
FileInfo fileInfo = new FileInfo(fileNamePath);
if ((fileInfo.Attributes & FileAttributes.Directory) != 0)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 删除指定文件夹
/// </summary>
/// <param name="dir">文件夹路径</param>
public static void DeleteFolder(string dirPath)
{
foreach (string d in Directory.GetFileSystemEntries(dirPath))
{
if (File.Exists(d))
{
FileInfo fi = new FileInfo(d);
if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
fi.Attributes = FileAttributes.Normal;
File.Delete(d);//直接删除其中的文件
}
else
{
DeleteFolder(d);//递归删除子文件夹
}
}
Directory.Delete(dirPath);//删除已空文件夹
}
}
}