c# 目录操作类
/// <summary>
/// 目录操作类
/// </summary>
public class DirectoryHelper
{
public DirectoryHelper()
{ }
/// <summary>
/// 得到所有磁盘驱动器
/// </summary>
/// <returns></returns>
public string[] AllDrivers()
{
return Directory.GetLogicalDrives();
}
/// <summary>
/// 应用程序的当前工作目录,bin文件夹
/// </summary>
/// <returns></returns>
public string CurrentDirectory()
{
return Directory.GetCurrentDirectory();
}
/// <summary>
/// 获取path目录下的文件,不包括文件夹
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public string[] AllFiles(string path)
{
return Directory.GetFiles(path);
}
/// <summary>
/// 创建一个目录如果directory存在,那么就不创建了目录了
/// </summary>
/// <param name="directory"></param>
public void CreateDirectory(string directory)
{
//DirectoryInfo info = new DirectoryInfo(directory);
//info.Create();
DirectoryInfo info = Directory.CreateDirectory(directory);
}
/// <summary>
/// 删除目录
/// </summary>
/// <param name="path"></param>
public void DeleteDirectory(string path)
{
//Directory.Delete(path, false/*删除子目录*/); //子目录非空则报错。
Directory.Delete(path, true/*保留子目录*/); //忽略子目录内容,有没有内容都删除
}
/// <summary>
/// 目录下的所有文件,隐藏文件也可见,偷窥吗?
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public string[] AllContent(string path)
{
return Directory.GetFileSystemEntries(path);//如果path不存在则报错
}
/// <summary>
/// 复制文件目录,复制完成原目录删除
/// </summary>
/// <param name="path1"></param>
/// <param name="path2"></param>
public void CopyDirectory(string sourcePath/*复制前必须存在,复制完成后挂掉了*/,string targetPath/*复制前不能存在*/)
{
Directory.Move(sourcePath,targetPath);
}
/// <summary>
/// 编码,说实话我没用过,不知道干吗用.
/// </summary>
/// <returns></returns>
public string Codeing()
{
Encoding code = Encoding.Default;
//byte[] bytes = code.GetBytes("中华人民共和国");
byte[] bytes = code.GetBytes("i'm a smart boy");
return Encoding.GetEncoding("UTF-8").GetString(bytes); //doc环境不支持中文吗?中文不能正常转回去,英文就可以。
}
}