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;
    }
    }

    菜鸟伤不起......

  • 相关阅读:
    linux转换win下乱码txt命令
    linux下vi命令大全详细版本
    ubuntu系统如何安装adb调试环境
    LeetCode136---只出现一次的数字
    微信发朋友圈--用例设计(转)
    微服务
    LeetCode1---两数之和
    python输出
    爬楼梯,N级楼梯有多少种走法?
    list数组排序---stream
  • 原文地址:https://www.cnblogs.com/yyzq/p/2347273.html
Copyright © 2011-2022 走看看