CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    Nov 2018
    Posts
    12

    How to copy also subfolders and their files from an certain folder?

    Can someone help how I can also copy user's desktop folder's subfolders and their files with the code below this text?
    For moment I can only copy files from " + computer + "\c$\Users\Desktop folder but not the subfolders and their files.

    Code:
      
       string[] dirs = Directory.GetDirectories("\\\\" + computer + "\\c$\\Users\\");
      foreach (string item in dirs)
        {
            FileInfo f = new FileInfo(item);
             user2 = "\\\\" + computer + "\\c$\\Users\\" + f.Name + "\\Desktop";
             if (Directory.Exists(user2))
                                {
             user = "C:\\Backup" + computer + "" + f.Name + "\\Desktop";
             Directory.CreateDirectory(user);
             DirectoryInfo folder = new DirectoryInfo(user2);
             FileInfo[] files = folder.GetFiles();
             foreach (FileInfo file in files)
             {
                   string temppath = Path.Combine(user, file.Name);
                    file.CopyTo(temppath, true);
              }
         }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to copy also subfolders and their files from an certain folder?

    You'll need to put that code inside a recursive method. Search bing or google for 'recursion DirectoryInfo C# example".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured