zoukankan      html  css  js  c++  java
  • C#复制文件夹

    昨天打算利用获取的文件夹下所有文件列表,然后在目标位置新建这个目录,后来发现很麻烦,实现起来有太多的无用的东西。在网上找了下高手的文章,无耻的用了人家的方法.......。但总的说还是递归的用法,只不过我没想到用DirectoryInfo这个类,一直在对着Directory想,Directory主要是静态方法,没有属性所以没法获取文件夹的更多信息。

    贴代码:

     public static bool Copy(string sourcePath, string targetPath)
    {
    bool b=false;
    try
    {
    if (Directory.Exists(sourcePath))
    {
    if (!Directory.Exists(targetPath))//判断目标路径是否存在,不存在则创建
    {
    Directory.CreateDirectory(targetPath);
    }
    DirectoryInfo sDir = new DirectoryInfo(sourcePath);
    DirectoryInfo[] dir = sDir.GetDirectories();
    FileInfo[] file = sDir.GetFiles();
    foreach (FileInfo f in file)//获取当前目录下的文件Copy到目标路径下
    {
    f.CopyTo(targetPath + "\\" + f.Name, true);
    }
    foreach (DirectoryInfo d in dir)//进入源目录的子目录进行递归.....
    {
    Copy(d.FullName, targetPath + "\\" + d.Name);
    }
    b = true;
    }
    return b;
    }
    catch {
    return b;
    }
    }

    菜鸟伤不起......

  • 相关阅读:
    poj3481
    查找湖南问题
    tyvj1033
    tyvj1088
    oil倒油
    加分二叉树
    模拟题2
    模拟题3
    Free pascal中的random函数
    Spring MVC入门配置
  • 原文地址:https://www.cnblogs.com/yyzq/p/2347273.html
Copyright © 2011-2022 走看看