我有这个MSDN的代码,但仍然无法让我的程序使用它 [英] i have got this code of MSDN but still can't get my program to work with it

查看:152
本文介绍了我有这个MSDN的代码,但仍然无法让我的程序使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从多个目录复制所有的文件,但我想做的是复制所有的目录与它们内的文件,因为他们是我复制,而不是只是我的目标文件夹中的文件。这是我的代码到目前为止

I can copy all the files from multiple directory's but what I want to do is copy all of the directory's with the files inside them as they are where I am copying the from and not putting just the files in my target folder. Here is my code so far

{
    string SelectedPath = (string)e.Argument;
    string sourceDirName;
    string destDirName;
    bool copySubDirs;
    DirectoryCopy(".", SelectedPath, true);

  DirectoryInfo dir = new DirectoryInfo(sourceDirName);
  DirectoryInfo[] dirs = dir.GetDirectories();

  // If the source directory does not exist, throw an exception.
    if (!dir.Exists)
    {
        throw new DirectoryNotFoundException(
            "Source directory does not exist or could not be found: "
            + sourceDirName);
    }

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


    // Get the file contents of the directory to copy.
    FileInfo[] files = dir.GetFiles();

    foreach (FileInfo file in files)
    {
        // Create the path to the new copy of the file.
        string temppath = Path.Combine(destDirName, file.Name);

        // Copy the file.
        file.CopyTo(temppath, false);
    }

    // If copySubDirs is true, copy the subdirectories.
    if (copySubDirs)
    {

        foreach (DirectoryInfo subdir in dirs)
        {
            // Create the subdirectory.
            string temppath = Path.Combine(destDirName, subdir.Name);

            // Copy the subdirectories.
            DirectoryCopy(subdir.FullName, temppath, copySubDirs);
        }
    }
}                

/ p>

any help will be appreciated

推荐答案

复制目录没有开箱即用的方法。你最好做的是使用扩展方法。请查看此 - http:// channel9 .msdn.com / Forums / TechOff / 257490-How-Copy-directories-in-C / 07964d767cc94c3990bb9dfa008a52c8

There is no out of the box method for copying directories. The best you can do is use Extension methods. Have a look at this - http://channel9.msdn.com/Forums/TechOff/257490-How-Copy-directories-in-C/07964d767cc94c3990bb9dfa008a52c8

下面是完整的示例它工作):

Here is the complete example (Just tested and it works):

using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var di = new DirectoryInfo("C:\\SomeFolder");
            di.CopyTo("E:\\SomeFolder", true);
        }
    }

public static class DirectoryInfoExtensions
{
    // Copies all files from one directory to another.
    public static void CopyTo(this DirectoryInfo source, string destDirectory, bool recursive)
    {
        if (source == null)
            throw new ArgumentNullException("source");
        if (destDirectory == null)
            throw new ArgumentNullException("destDirectory");

        // If the source doesn't exist, we have to throw an exception.
        if (!source.Exists)
            throw new DirectoryNotFoundException("Source directory not found: " + source.FullName);
        // Compile the target.
        DirectoryInfo target = new DirectoryInfo(destDirectory);
        // If the target doesn't exist, we create it.
        if (!target.Exists)
            target.Create();

        // Get all files and copy them over.
        foreach (FileInfo file in source.GetFiles())
        {
            file.CopyTo(Path.Combine(target.FullName, file.Name), true);
        }

        // Return if no recursive call is required.
        if (!recursive)
            return;

        // Do the same for all sub directories.
        foreach (DirectoryInfo directory in source.GetDirectories())
        {
            CopyTo(directory, Path.Combine(target.FullName, directory.Name), recursive);
        }
    }
}

}

这篇关于我有这个MSDN的代码,但仍然无法让我的程序使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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