SoFunction
Updated on 2025-03-01

C# implements WPF project copying and moving folders

A simple demo of operating files made using WPF, including copying and moving folders. The core idea is to use recursion. If you just move or copy a single file, just use the () or () method directly.

XAML Code

<Window x:Class=""
        xmlns="/winfx/2006/xaml/presentation"
        xmlns:x="/winfx/2006/xaml"
        xmlns:d="/expression/blend/2008"
        xmlns:mc="/markup-compatibility/2006"
        xmlns:local="clr-namespace:OperationFile"
        mc:Ignorable="d"
        Title="MainWindow" Height="220" Width="300">
    <Grid>
        <Button Content="Copy File" HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" Width="100" Margin="80,20"
                x:Name="btnCopy" Click="btnCopy_Click"/>

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

The backend code is as follows:

using System;
using ;

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

        /// <summary>
        /// Move folder (copy)        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMove_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                (@"C:\Test", @"D:\Raw Data\Temp");
                ("Move folders done");
            }
            catch (Exception ex)
            {
                ("Error moving folder");
            }
        }

        /// <summary>
        /// Copy folder        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                (@"C:\Test", @"D:\Raw Data\Temp");
                ("Copy folder complete");
            }
            catch (Exception ex)
            {
                ("Error copying folder");
            }
        }
    }
}

FileUtility class code

using System;
using ;

namespace OperationFile
{
    public static class FileUtility
    {
        /// <summary>
        /// Copy folders and files        /// </summary>
        /// <param name="sourceFolder">original file path</param>        /// <param name="destFolder">Target file path</param>        /// &lt;returns&gt;&lt;/returns&gt;
        public static void CopyFolder(string sourceFolder, string destFolder)
        {
            try
            {
                //If the target path does not exist, create the target path                if (!(destFolder))
                {
                    (destFolder);
                }
                //Get all files in the root directory of the original file                string[] files = (sourceFolder);
                foreach (string file in files)
                {
                    string name = (file);
                    string dest = (destFolder, name);
                    // Copy the file                    (file, dest);
                }
                //Get all folders in the root directory of the original file                string[] folders = (sourceFolder);
                foreach (string folder in folders)
                {
                    string dirName = ('\\')[('\\').Length - 1];
                    string destfolder = (destFolder, dirName);
                    // Recursive call                    CopyFolder(folder, destfolder);
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"copy file Error:{}\r\n source:{}");
            }
        }


        /// &lt;summary&gt;
        /// Move files        /// &lt;/summary&gt;
        /// <param name="sourceFolder">source folder</param>        /// <param name="destFolder">What about the target file</param>        public static void MoveFolder(string sourceFolder, string destFolder)
        {
            try
            {
                //If the target path does not exist, create the target path                if (!(destFolder))
                {
                    (destFolder);
                }
                //Get all files in the root directory of the original file                string[] files = (sourceFolder);
                foreach (string file in files)
                {
                    string name = (file);
                    string dest = (destFolder, name);
                    // Move files                    (file, dest);
                }
                //Get all folders in the root directory of the original file                string[] folders = (sourceFolder);
                foreach (string folder in folders)
                {
                    string dirName = ('\\')[('\\').Length - 1];
                    string destfolder = (destFolder, dirName);
                    // Recursive call                    MoveFolder(folder, destfolder);
                }

                // Delete the source folder                (sourceFolder);
            }
            catch (Exception ex)
            {
                throw new Exception($"move file Error:{}\r\n source:{}");
            }
        }
    }
}

This is all about this article about C#’s implementation of WPF project copying and moving folders. I hope it will be helpful to everyone's learning and I hope everyone will support me more.