C# 删除 或移动 文件夹及文件夹内的内容。

18 篇文章 0 订阅
/// <summary>
        /// 复制文件夹(及文件夹下所有子文件夹和文件)
        /// </summary>
        /// <param name="sourcePath">待移动的文件夹路径</param>
        /// <param name="destinationPath">目标路径</param>
        public static void CopyDirectory(String sourcePath, String destinationPath)
        {
            var info = new DirectoryInfo(sourcePath);
            Directory.CreateDirectory(destinationPath);

            foreach (var fsi in info.GetFileSystemInfos())
            {
                var destName = Path.Combine(destinationPath, fsi.Name);

                //如果是文件,移动文件  Ps : 如果复制 使用Copy
                if (fsi is FileInfo)         
                    File.Move(fsi.FullName, destName);
                //如果是文件夹,新建文件夹,递归                     
                else               
                {
                    Directory.CreateDirectory(destName);
                    CopyDirectory(fsi.FullName, destName);
                }
            }

            //删除旧文件夹
            DeleteFolder(sourcePath);
        }
       
        /// <summary>
        /// 删除文件夹(及文件夹下所有子文件夹和文件)
        /// </summary>
        /// <param name="deleteDirectory">删除文件夹的路径</param>
        public static void DeleteFolder(string deleteDirectory)
        {
            if (!Directory.Exists(deleteDirectory)) return;

            foreach (var deleteFile in Directory.GetFileSystemEntries(deleteDirectory))
            {
                if (File.Exists(deleteFile))
                    File.Delete(deleteFile);
                else
                    DeleteFolder(deleteFile);
            }

            Directory.Delete(deleteDirectory);
        }


 

 

附带一个微软官方的Demo :

下面的示例演示如何复制文件和目录。

// Simple synchronous file copy operations with no user interface.
// To run this sample, first create the following directories and files:
// C:\Users\Public\TestFolder
// C:\Users\Public\TestFolder\test.txt
// C:\Users\Public\TestFolder\SubDir\test.txt
public class SimpleFileCopy
{
    static void Main()
    {
        string fileName = "test.txt";
        string sourcePath = @"C:\Users\Public\TestFolder";
        string targetPath =  @"C:\Users\Public\TestFolder\SubDir";

        // Use Path class to manipulate file and directory paths.
        string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
        string destFile = System.IO.Path.Combine(targetPath, fileName);

        // To copy a folder's contents to a new location:
        // Create a new target folder, if necessary.
        if (!System.IO.Directory.Exists(targetPath))
        {
            System.IO.Directory.CreateDirectory(targetPath);
        }

        // To copy a file to another location and 
        // overwrite the destination file if it already exists.
        System.IO.File.Copy(sourceFile, destFile, true);

        // To copy all the files in one directory to another directory.
        // Get the files in the source folder. (To recursively iterate through
        // all subfolders under the current directory, see
        // "How to: Iterate Through a Directory Tree.")
        // Note: Check for target path was performed previously
        //       in this code example.
        if (System.IO.Directory.Exists(sourcePath))
        {
            string[] files = System.IO.Directory.GetFiles(sourcePath);

            // Copy the files and overwrite destination files if they already exist.
            foreach (string s in files)
            {
                // Use static Path methods to extract only the file name from the path.
                fileName = System.IO.Path.GetFileName(s);
                destFile = System.IO.Path.Combine(targetPath, fileName);
                System.IO.File.Copy(s, destFile, true);
            }
        }
        else
        {
            Console.WriteLine("Source path does not exist!");
        }

        // Keep console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}


更多微软Demo 请参考:http://msdn.microsoft.com/zh-cn/library/cc148994.aspx

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值