如何使用C#将目录从一个位置复制到另一个位置 [英] How to copy a directory from one location to another using C#

查看:86
本文介绍了如何使用C#将目录从一个位置复制到另一个位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录多个内部目录和文件。

我需要使用C#将包含所有文件的目录复制到另一个位置。



怎么做

I am having a directory multiple inner directories and files.
I need to copy the directory having all the files to another location using C#.

How to do this

推荐答案

谷歌是你的朋友:好好经常拜访他。他可以比在这里发布问题更快地回答问题...



快速搜索结果超过300万: Google目录副本c# [ ^ ]

最重要的结果是MSDN的完整示例: http://msdn.microsoft.com/en-us/library/bb762914(v = vs.110).aspx [ ^ ]



将来,请尝试自己做至少基础研究,不要浪费你时间或我们的。
Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A very quick search gave over 3 million results: Google "directory copy c#"[^]
The top result was MSDN with a full example: http://msdn.microsoft.com/en-us/library/bb762914(v=vs.110).aspx[^]

In future, please try to do at least basic research yourself, and not waste your time or ours.


如何:复制,删除和移动文件和文件夹


您好

试试这个示例代码;



Hi
Try like this sample code;

using System;
using System.IO;

class DirectoryCopyExample
{
    static void Main()
    {
        // Copy from the current directory, include subdirectories.
        DirectoryCopy(".", @".\temp", true);
    }

    private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
    {
        // Get the subdirectories for the specified directory.
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);
        DirectoryInfo[] dirs = dir.GetDirectories();

        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException(
                "Source directory does not exist or could not be found: "
                + sourceDirName);
        }

        // If the destination directory doesn't exist, create it.
        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }

        // Get the files in the directory and copy them to the new location.
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            string temppath = Path.Combine(destDirName, file.Name);
            file.CopyTo(temppath, false);
        }

        // If copying subdirectories, copy them and their contents to new location.
        if (copySubDirs)
        {
            foreach (DirectoryInfo subdir in dirs)
            {
                string temppath = Path.Combine(destDirName, subdir.Name);
                DirectoryCopy(subdir.FullName, temppath, copySubDirs);
            }
        }
    }
}


这篇关于如何使用C#将目录从一个位置复制到另一个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆