i have got this code of MSDN but still can't get my program to work with it

♀尐吖头ヾ 提交于 2019-12-25 14:00:18

问题


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);
        }
    }
}                

any help will be appreciated


回答1:


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);
        }
    }
}

}




回答2:


Have you taken a look at http://msdn.microsoft.com/en-us/library/bb762914.aspx ?




回答3:


Maybe try to see if the path exists before the copy. If it isn't there, then create it?

string folderPath = Path.GetDirectoryName(path);
if (!Directory.Exists(folderPath))
    Directory.CreateDirectory(folderPath);



回答4:


The smart way to do it is as in nithins answer. Using your approach you could use the FileInfo.Directory information to determine the source of the file and then create this directory in your destination if required. But nithins link is a cleaner solution.



来源:https://stackoverflow.com/questions/4900756/i-have-got-this-code-of-msdn-but-still-cant-get-my-program-to-work-with-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!