zoukankan      html  css  js  c++  java
  • C#将文件从指定的目录复制到另一个目录

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;

    namespace TEST
    {
    class FileHelper
    {/// <summary>
    /// 复制目录
    /// </summary>
    /// <param name="SourcePath">源路径</param>
    /// <param name="TargetPath">目标路径</param>
    /// <param name="Overwrite">是否覆盖</param>
    public static bool CopyDirectory(string SourcePath, string TargetPath, bool Overwrite)
    {
    // 如果源目录不存在,则退出
    if (!Directory.Exists(SourcePath))
    {
    return false;
    }

    // 检查目标目录是否以目录分割字符结束如果不是则添加
    if (TargetPath[TargetPath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
    {
    TargetPath += System.IO.Path.DirectorySeparatorChar;
    }
    try
    {

    // 如果目标路径不存在,则创建此文件夹
    if (!Directory.Exists(TargetPath))
    {
    Directory.CreateDirectory(TargetPath);
    }
    }
    catch (Exception ex)
    {
    string ErrInfo = ex.Message;
    return false;
    }
    if (Directory.Exists(TargetPath))
    {

    // 遍历源路径的文件夹,获取文件名(带路径的)
    foreach (string FileName in Directory.GetFiles(SourcePath))
    {
    try
    {

    //复制文件
    File.Copy(FileName, Path.Combine(TargetPath, Path.GetFileName(FileName)), Overwrite);
    }
    catch (Exception ex)
    {
    string ErrInfo = ex.Message;
    }
    }

    // 子文件夹的遍历

    foreach (string SubPath in Directory.GetDirectories(SourcePath))
    {

    //复制文件
    CopyDirectory(SubPath, Path.Combine(TargetPath, Path.GetFileName(SubPath)), Overwrite);
    }
    }
    return true;

    }
    }
    }

  • 相关阅读:
    OpenJudge百炼习题解答(C++)--题4010:2011
    Centos6.5卸载图形化
    nfs远程挂载问题记录
    走马观花-浪里跳-学习英文
    weblogic部署存在中文乱码导致部署失败
    KMS11激活Window系列
    mysql8.x开启远程登录
    notepad++插件实现json、xml格式化
    RHEL SHELL快捷键
    linux-env命令解析
  • 原文地址:https://www.cnblogs.com/leku_cc/p/2908905.html
Copyright © 2011-2022 走看看