N
N
networkSolutions2018-05-16 06:19:15
C++ / C#
networkSolutions, 2018-05-16 06:19:15

How to copy selected folders C#?

You need to copy folders with files in them.
For example, there is a function:

void perebor_updates(string begin_dir, string end_dir)
        {
            DirectoryInfo dir_inf = new DirectoryInfo(begin_dir);
            foreach (DirectoryInfo dir in dir_inf.GetDirectories())
            {
                if (Directory.Exists(end_dir + "\\" + dir.Name) != true)
                {
                    Directory.CreateDirectory(end_dir + "\\" + dir.Name);
                }
                
                perebor_updates(dir.FullName, end_dir + "\\" + dir.Name);
            }

            foreach (string file in Directory.GetFiles(begin_dir))
            {
                string filik = file.Substring(file.LastIndexOf('\\'), file.Length - file.LastIndexOf('\\'));
                File.Copy(file, end_dir + "\\" + filik, true);
            }
        }

I need to go through not all directories
foreach (DirectoryInfo dir in dir_inf.GetDirectories())

, but only for the selected ones, that is, those that I will pass to the function.
For example, a directory is given, it contains folders with files, and I pass only those folders that I want to copy to the function.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2018-05-16
@networkSolutions

On input IEnumerable or IEnumerable. Then:

void perebor_updates(IEnumerable<DirectoryInfo> dirs)
{
  foreach(var dir in dirs)
...
}

or
void perebor_updates(IEnumerable<string> dirNames)
{
  foreach(var name in dirNames)
  {
    var dir = new DirectoryInfo(name);
...
  {
...
}

And then you have a recursive function that takes one folder as a parameter and performs the desired action.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question