Autor Beitrag
OKNER
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 19



BeitragVerfasst: Mo 21.11.05 15:16 
Hallo,

wie kann ich komplette Vezeichnisse mit c# kopieren (inkl Unterverzeichnisse; Rekursion)?

Hat jemand ein Beispielß

Gruß
Renko
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mo 21.11.05 20:11 
Hallo!

Du hast das Stichwort ja schon selber genannt: Rekursion. Du ermittelst über Directory bzw. DirectoryInfo die in einem Verzeichnis enthaltenen Verzeichnisse und Dateien. Die Dateien kopierst Du einfach, bei den Verzeichnissen extrahierst Du den Namen und erstellst sie am Zielort. Dann gehst Du die Verzeichnisse durch und fängst in jedem Verzeichnis von vorne an.

Grüße
Christian

P.S.: Benötigter Namespace: System.IO

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
OKNER Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 19



BeitragVerfasst: Di 22.11.05 10:44 
Titel: Danke für den Tipp
Das werde ich gleich mal probieren.
OKNER Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 19



BeitragVerfasst: Di 22.11.05 18:23 
Titel: Beispiel
Hi,

so funktioniert es. Danke.


ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
  /// <summary>
  /// Zusammenfassung für Class1.
  /// </summary>
  class Class1
  {
    [STAThread]
    static void Main(string[] args)
    {
      myFilesystem x = new myFilesystem();

      DirectoryInfo di = new DirectoryInfo("d:\\test");

      x.CopyFolder(di , "d:\\test5");
    }
  
  }
  
  class myFilesystem
  {
    public bool CopyFolder(DirectoryInfo source, string dest) 
    {
      bool result = true;

      if(!Directory.Exists(dest))
        Directory.CreateDirectory(dest);

      foreach(FileInfo fi in source.GetFiles()) 
      {
        string destFile = dest + "\\" + fi.Name;
        result = result && (fi.CopyTo(destFile, true) != null);
      }

      foreach(DirectoryInfo subDir in source.GetDirectories())
      {
        string destTmp = subDir.FullName.Replace(source.FullName, dest);
        result = result && CopyFolder(subDir, destTmp);
          
      }
      return result;

    }
  }


Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt