{"id":1680,"date":"2016-03-07T11:29:35","date_gmt":"2016-03-07T10:29:35","guid":{"rendered":"http:\/\/www.capri-soft.de\/blog\/?p=1680"},"modified":"2016-03-07T11:38:16","modified_gmt":"2016-03-07T10:38:16","slug":"net-c-verzeichnis-rekursiv-kopieren-copy-folder-with-subfolders-recursive","status":"publish","type":"post","link":"https:\/\/www.capri-soft.de\/blog\/?p=1680","title":{"rendered":".NET C# : Verzeichnis rekursiv kopieren \/ Copy folder with subfolders (recursive)"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>A directory folder with subfolders should be copied. Ein Verzeichnis mit allen Unterverzeichnissen soll rekursiv kopiert warden.<\/p>\n<h1>Approach<\/h1>\n<p>Usage of DirectoryInfo Class: getDirectories(); in combination with &#8222;foreach (DirectoryInfo subdir in dirs)&#8220;-Loop<br \/>\nUsage of FileInfo Class: getFiles() in combination with &#8222;foreach (FileInfo file in files)&#8220;-Loop and Object-Method &#8222;file.CopyTo(DESTINATION_FOLDER, true);&#8220;<\/p>\n<h1>Solution &#8211; L\u00f6sung<\/h1>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\npublic void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)\r\n{\r\n    \/\/ Get the subdirectories for the specified directory.\r\n    DirectoryInfo dir = new DirectoryInfo(sourceDirName);\r\n    DirectoryInfo&#x5B;] dirs = dir.GetDirectories();\r\n\r\n    if (!dir.Exists)\r\n    {\r\n        throw new DirectoryNotFoundException(\r\n            &quot;Source directory does not exist or could not be found: &quot;\r\n            + sourceDirName);\r\n    }\r\n\r\n    \/\/ If the destination directory doesn&#039;t exist, create it.\r\n    if (!Directory.Exists(destDirName))\r\n    {\r\n        Directory.CreateDirectory(destDirName);\r\n    }\r\n\r\n    \/\/ Get the files in the directory and copy them to the new location.\r\n    FileInfo&#x5B;] files = dir.GetFiles();\r\n    foreach (FileInfo file in files)\r\n    {\r\n        string temppath = Path.Combine(destDirName, file.Name);\r\n        file.CopyTo(temppath, true);\r\n    }\r\n\r\n    \/\/ If copying subdirectories, copy them and their contents to new location.\r\n    if (copySubDirs)\r\n    {\r\n        foreach (DirectoryInfo subdir in dirs)\r\n        {\r\n            string temppath = Path.Combine(destDirName, subdir.Name);\r\n            DirectoryCopy(subdir.FullName, temppath, copySubDirs);\r\n        }\r\n    }\r\n}<\/pre>\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https%3A%2F%2Fwww.capri-soft.de%2Fblog%2F%3Fp%3D1680&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px;margin-top:5px;\"><\/iframe>","protected":false},"excerpt":{"rendered":"<p>Problem A directory folder with subfolders should be copied. Ein Verzeichnis mit allen Unterverzeichnissen soll rekursiv kopiert warden. Approach Usage of DirectoryInfo Class: getDirectories(); in combination with &#8222;foreach (DirectoryInfo subdir in dirs)&#8220;-Loop Usage of FileInfo Class: getFiles() in combination with &#8222;foreach (FileInfo file in files)&#8220;-Loop and Object-Method &#8222;file.CopyTo(DESTINATION_FOLDER, true);&#8220; Solution &#8211; L\u00f6sung public void DirectoryCopy(string &hellip; <a href=\"https:\/\/www.capri-soft.de\/blog\/?p=1680\" class=\"more-link\"><span class=\"screen-reader-text\">.NET C# : Verzeichnis rekursiv kopieren \/ Copy folder with subfolders (recursive)<\/span> weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,33,3],"tags":[],"class_list":["post-1680","post","type-post","status-publish","format-standard","hentry","category-net","category-c","category-programmierung"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4yGeN-r6","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1680","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1680"}],"version-history":[{"count":3,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1680\/revisions"}],"predecessor-version":[{"id":1683,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1680\/revisions\/1683"}],"wp:attachment":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}