My Getdirectories() only brings a directory, when there is 5

Asked

Viewed 24 times

0

I have that line.

DirectoryInfo dir = new DirectoryInfo(sourceDirName);

Then I try to get all the subfolders, like this:

DirectoryInfo[] dirs = dir.GetDirectories();

The Array dirs is only loaded with a subfolder and not with the 5. Below my full code:

private void CopyRenameFolder(string sourceDirName, string destDirName, bool copySubDirs)
        {
            // Get the subdirectories for the specified directory.
            DirectoryInfo dir = new DirectoryInfo(sourceDirName);

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

            DirectoryInfo[] dirs = dir.GetDirectories();
            //string[] dirs = Directory.GetDirectories(sourceDirName, "*", SearchOption.AllDirectories);
            // 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);
                    CopyRenameFolder(subdir.FullName, temppath, copySubDirs);
                }
            }
        }
  • The issue there is not files but folders, so another post. I’m not interested in files in this post but folders, but I’ve solved and will post the resolution.

  • Bigown, before you do anything, you should read the post and see what it seeks and I didn’t go out doing things for what you didn’t understand.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.