亲宝软件园·资讯

展开

C#复制和移动文件夹

.NET开发菜鸟 人气:0

使用WPF做的一个简单的操作文件的demo,包括复制和移动文件夹,核心思想就是使用递归,如果只是移动或者复制单一文件,直接使用File.Copy()或者File.Move()方法即可。

XAML代码

<Window x:Class="OperationFile.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:OperationFile"
        mc:Ignorable="d"
        Title="MainWindow" Height="220" Width="300">
    <Grid>
        <Button Content="复制文件" HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" Width="100" Margin="80,20"
                x:Name="btnCopy" Click="btnCopy_Click"/>

        <Button Content="移动文件" HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" Width="100" Margin="80,70"
                x:Name="btnMove" Click="btnMove_Click"/>
    </Grid>
</Window>

后端代码如下:

using System;
using System.Windows;

namespace OperationFile
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 移动文件夹(拷贝)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMove_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                FileUtility.MoveFolder(@"C:\Test", @"D:\Raw Data\Temp");
                MessageBox.Show("移动文件夹完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show("移动文件夹出错");
            }
        }

        /// <summary>
        /// 复制文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                FileUtility.CopyFolder(@"C:\Test", @"D:\Raw Data\Temp");
                MessageBox.Show("复制文件夹完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show("复制文件夹出错");
            }
        }
    }
}

FileUtility类代码

using System;
using System.IO;

namespace OperationFile
{
    public static class FileUtility
    {
        /// <summary>
        /// 复制文件夹及文件
        /// </summary>
        /// <param name="sourceFolder">原文件路径</param>
        /// <param name="destFolder">目标文件路径</param>
        /// <returns></returns>
        public static void CopyFolder(string sourceFolder, string destFolder)
        {
            try
            {
                //如果目标路径不存在,则创建目标路径
                if (!Directory.Exists(destFolder))
                {
                    Directory.CreateDirectory(destFolder);
                }
                //得到原文件根目录下的所有文件
                string[] files = Directory.GetFiles(sourceFolder);
                foreach (string file in files)
                {
                    string name = Path.GetFileName(file);
                    string dest = Path.Combine(destFolder, name);
                    // 复制文件
                    File.Copy(file, dest);
                }
                //得到原文件根目录下的所有文件夹
                string[] folders = Directory.GetDirectories(sourceFolder);
                foreach (string folder in folders)
                {
                    string dirName = folder.Split('\\')[folder.Split('\\').Length - 1];
                    string destfolder = Path.Combine(destFolder, dirName);
                    // 递归调用
                    CopyFolder(folder, destfolder);
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"copy file Error:{ex.Message}\r\n source:{ex.StackTrace}");
            }
        }


        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="sourceFolder">源文件夹</param>
        /// <param name="destFolder">目标文件呢</param>
        public static void MoveFolder(string sourceFolder, string destFolder)
        {
            try
            {
                //如果目标路径不存在,则创建目标路径
                if (!Directory.Exists(destFolder))
                {
                    Directory.CreateDirectory(destFolder);
                }
                //得到原文件根目录下的所有文件
                string[] files = Directory.GetFiles(sourceFolder);
                foreach (string file in files)
                {
                    string name = Path.GetFileName(file);
                    string dest = Path.Combine(destFolder, name);
                    // 移动文件
                    File.Move(file, dest);
                }
                //得到原文件根目录下的所有文件夹
                string[] folders = Directory.GetDirectories(sourceFolder);
                foreach (string folder in folders)
                {
                    string dirName = folder.Split('\\')[folder.Split('\\').Length - 1];
                    string destfolder = Path.Combine(destFolder, dirName);
                    // 递归调用
                    MoveFolder(folder, destfolder);
                }

                // 删除源文件夹
                Directory.Delete(sourceFolder);
            }
            catch (Exception ex)
            {
                throw new Exception($"move file Error:{ex.Message}\r\n source:{ex.StackTrace}");
            }
        }
    }
}

到此这篇关于C#实现WPF项目复制和移动文件夹的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论