-4

I am using the function on this SO post to copy folders content into another folder, but it's not copying the sub folders and their content.

private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
    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))
    {
        Debug.Log("Directory created.." + 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);
        }
    }
}

This is how it's called:

string destingationPath = startupFolder + @"\NetworkingDemoPlayerWithNetworkAwareShooting1_Data";
DirectoryCopy("NetworkingDemoPlayerWithNetworkAwareShooting1_Data", destingationPath, true);
Community
  • 1
  • 1
Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186
  • 4
    Possible duplicate of [Best way to copy the entire contents of a directory in C#](http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c-sharp) – BartoszKP Oct 19 '16 at 11:42
  • 2
    Your code worked perfectly fine for me... It copied all directories and sub directories. Could you please check the path in calling method once? – A3006 Oct 19 '16 at 11:47
  • I guess this exception hindering the copy IOException: Win32 IO returned 112. – Muhammad Faizan Khan Oct 19 '16 at 11:56
  • @Anand yes code is correct there was storage problem cristallo mentioned it. thanks – Muhammad Faizan Khan Oct 19 '16 at 12:26

1 Answers1

1

I agree that your code looks correct

Windows system error 112 means there is not enough space on the disk

cristallo
  • 1,951
  • 2
  • 25
  • 42