sardine254

        
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

创建文件夹 复制文件 删除文件

Posted on 2006-03-24 22:36  sardine  阅读(278)  评论(0)    收藏  举报

public class FileClass
 {
  SqlClass myclass =new SqlClass();
  public FileClass()
  {
   // TODO: 在此处添加构造函数逻辑
  }  
  public void createfolder(string path)
  {
   if(!Directory.Exists(path))
   {
    Directory.CreateDirectory(path);
    //对创建的文件夹设置去权限
    SecurityDescriptor secDesc = SecurityDescriptor.GetFileSecurity(path,SECURITY_INFORMATION.DACL_SECURITY_INFORMATION);    
    secDesc.Dacl.AddAce(new AceAccessAllowed(Sids.Self, AccessType.GENERIC_WRITE|AccessType.GENERIC_EXECUTE|AccessType.GENERIC_ALL|AccessType.WRITE_OWNER));
   }
  }
  
  public  void CopyDir(string srcPath,string aimPath)
  {
   try
   {
    // 检查目标目录是否以目录分割字符结束如果不是则添加之
    if(aimPath[aimPath.Length-1] != Path.DirectorySeparatorChar)
     aimPath += Path.DirectorySeparatorChar;
    // 判断目标目录是否存在如果不存在则新建之
    if(!Directory.Exists(aimPath)) Directory.CreateDirectory(aimPath);
    // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
    // 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
    // string[] fileList = Directory.GetFiles(srcPath);
    string[] fileList = Directory.GetFileSystemEntries(srcPath);
    // 遍历所有的文件和目录
    foreach(string file in fileList)
    {
     // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
     if(Directory.Exists(file))
      CopyDir(file,aimPath+Path.GetFileName(file));
      // 否则直接Copy文件
     else
      File.Copy(file,aimPath+Path.GetFileName(file),true);
    }
   }
   catch (Exception e)
   {
    //MessageBox.Show (e.ToString());
   }
  }

  public  void DeleteDir(string aimPath)
  {
   try
   {
    // 检查目标目录是否以目录分割字符结束如果不是则添加之
    if(aimPath[aimPath.Length-1] != Path.DirectorySeparatorChar)
     aimPath += Path.DirectorySeparatorChar;
    // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
    // 如果你指向Delete目标文件下面的文件而不包含目录请使用下面的方法
    // string[] fileList = Directory.GetFiles(aimPath);
    string[] fileList = Directory.GetFileSystemEntries(aimPath);
    // 遍历所有的文件和目录
    foreach(string file in fileList)
    {
     // 先当作目录处理如果存在这个目录就递归Delete该目录下面的文件
     if(Directory.Exists(file))
     {
      DeleteDir(aimPath+Path.GetFileName(file));
     }
      // 否则直接Delete文件
     else
     {
      File.Delete (aimPath+Path.GetFileName(file));
     }
    }
    //删除文件夹
    System.IO .Directory .Delete (aimPath,true);
   }
   catch (Exception e)
   {
    //MessageBox.Show (e.ToString());
   }
  }