C# 基础(二十七)文件操作:Copy复制一个文件夹的所有内容到另一个文件夹、删除一个文件夹的所有内容

目录

第一章、简介

第二章、文件拷贝

第三章、文件删除


第一章、简介

       本篇博客,记录对文件、文件夹的日常拷贝、查找、删除等等功能。

第二章、文件拷贝

       Copy一个文件夹的所有内容到另一个文件夹

 

参考:

https://blog.csdn.net/jhoneyan/article/details/52689146

注意该程序,一直在执行递归操作

注意该程序,一直在执行递归操作

注意该程序,一直在执行递归操作

source code(实测过,可以用):

#region 将文件夹拷贝到另一个文件夹
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Messaging;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace test
{
    class Program
    {

        static void Main(string[] args)
        {

            string pLocalFilePath = @"C:\Users\lanmage2\Desktop\aaaa";//要复制的文件路径
            string pSaveFilePath = @"C:\Users\lanmage2\Desktop\bbbb";//指定存储的路径
            CopyDirectory(pLocalFilePath, pSaveFilePath);
        }

        /// <summary>
        /// 复制文件夹中的所有内容
        /// </summary>
        /// <param name="sourceDirPath">源文件夹目录</param>
        /// <param name="saveDirPath">指定文件夹目录</param>
        public static void CopyDirectory(string sourceDirPath, string saveDirPath)
        {
            try
            {
                //如果指定的存储路径不存在,则创建该存储路径
                if (!Directory.Exists(saveDirPath))
                {
                    //创建
                    Directory.CreateDirectory(saveDirPath);
                }

                //获取源路径文件的名称
                string[] files = Directory.GetFiles(sourceDirPath);

                //遍历子文件夹的所有文件。
                foreach (string file in files)
                {
                    string pFilePath = saveDirPath + "\\" + Path.GetFileName(file);
                    if (File.Exists(pFilePath))
                        continue;
                    File.Copy(file, pFilePath, true);
                }

                string[] dirs = Directory.GetDirectories(sourceDirPath);
                
                //递归,遍历文件夹
                foreach (string dir in dirs)
                {
                    CopyDirectory(dir, saveDirPath + "\\" + Path.GetFileName(dir));
                }
            }
            catch (Exception ex)
            {
                
            }
        }  
        
    }
}
#endregion 

第三章、文件删除

      /// <summary>
        /// 删除指定文件夹下的所有文件,但不删除文件夹
        /// </summary>
        /// <param name="strFolderPath"></param>
        public void DeleteFolderFileFunc(string strFolderPath)
        {
            try
            {
                DirectoryInfo dyInfo = new DirectoryInfo(strFolderPath);
                foreach (FileInfo feInfo in dyInfo.GetFiles())
                {
                    判断文件日期是否小于今天,是则删除
                    //if (feInfo.CreationTime < DateTime.Today)
                    //    feInfo.Delete();
                    feInfo.Delete();
                }
            }
            catch (Exception ex)
            {
                //log.WriteErrorLogFunc("FileManagement.cs::DeleteFolderFileFunc()——" + "删除指定文件夹下的所有文件发生错误:" + ex.ToString() + "\n\n");
            }
        }

        /// <summary>
        /// 删除指定文件夹
        /// </summary>
        /// <param name="strFolderPath"></param>
        public void DeleteFolderFunc(string strFolderPath)
        {
            try
            {
                if (Directory.Exists(strFolderPath))
                {
                    Directory.Delete(strFolderPath, true);
                }         
            }
            catch (Exception ex)
            {
                //log.WriteErrorLogFunc("FileManagement.cs::DeleteFolderFunc()——" + "删除指定文件夹发生错误:" + ex.ToString() + "\n\n");
            }
        }

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我爱AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值