Created
October 2, 2019 23:20
-
-
Save kyrathasoft/f5b054627d93bf49bf9eaf41b582edd5 to your computer and use it in GitHub Desktop.
demonstrates method CopyTo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* shows how to count the number of top-level | |
| subdirectories within a given directory | |
| online at: | |
| https://gist.github.com/kyrathasoft/d130f93dce7fb45341d18e6872b192cd | |
| To compile, use build.bat. Or, at the command line: | |
| csc /out:copydir.exe *.cs | |
| Here is the structure of this example program | |
| and associated directories, on disk: | |
| examples/CopyDirToDir | |
| - copyDir.cs (file) | |
| - boo | |
| - chirp (subdirectory) | |
| * tweet (file) | |
| * peep (file) | |
| - squawk (subdirectory) | |
| - buzzard (subdirectory) | |
| * caw (file) | |
| The above directory structure will be mirrored to whatever directory | |
| you create. | |
| */ | |
| using System; | |
| using System.IO; | |
| namespace Example { | |
| class Program { | |
| static void Main(string[] args){ | |
| string name = "boo"; | |
| while(name == "boo"){ | |
| Console.Clear(); | |
| Console.Write("\n Please specify the name of a directory you'd like to create (not 'boo'): "); | |
| name = Console.ReadLine().Trim().Replace(' ','_'); | |
| if(name.Length < 1){ | |
| name = "dest"; | |
| }else{ | |
| if(name.Length > 8){ | |
| name = name.Substring(0,8); | |
| } | |
| } | |
| } | |
| if(Directory.Exists(name)){ | |
| Console.WriteLine(" Directory '{0}' already exists.", name); | |
| }else{ | |
| Directory.CreateDirectory(name); | |
| Console.WriteLine(" Thank you. Directory '{0}' has been created.", name); | |
| } | |
| Console.WriteLine(" The program will copy the contents of 'boo' to '{0}'", name); | |
| try{ | |
| bool result = CopyTo("boo", name); | |
| if(result){ | |
| Console.WriteLine(" The contents of 'boo' were successfully copied to '{0}'", name); | |
| } | |
| }catch(Exception ex){ | |
| Console.WriteLine(" An error occurred:\n {0}", ex.Message); | |
| } | |
| PressToExit(); | |
| } | |
| public static bool CopyTo(string targetDirectory, string destinationDirectory) | |
| { | |
| bool blnSuccess = false; | |
| try{ | |
| if(Directory.Exists(targetDirectory)){ | |
| foreach (string dirPath in Directory.GetDirectories(targetDirectory, "*", | |
| SearchOption.AllDirectories)){ | |
| Directory.CreateDirectory(dirPath.Replace(targetDirectory, destinationDirectory)); | |
| } | |
| //Copy all the files & Replaces any files with the same name | |
| foreach (string newPath in Directory.GetFiles(targetDirectory, "*.*", | |
| SearchOption.AllDirectories)){ | |
| File.Copy(newPath, newPath.Replace(targetDirectory, destinationDirectory), true); | |
| blnSuccess = true; | |
| } | |
| } | |
| }catch(Exception exCopyTo){ | |
| Console.WriteLine("Error in ioLibrary: " + exCopyTo.Message); | |
| } | |
| return blnSuccess; | |
| } | |
| static void PressToExit(){ | |
| Console.Write(" Press any key to exit... "); | |
| Console.ReadKey(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment