0

Okay, so I have a text box in my program where a user can enter a value, however I am wanting to call this value in another class.

 public static void Main() { string sourceDirectory = @"F:\RootFolder\testingfolder\Test"; string targetDirectory = @"c:\targetDirectory"; //this is where the value would site Copy(sourceDirectory, targetDirectory); } 

Not 100% sure on how to call this. Edit After some much needed research I have found the below to work for me;

 private void CopyInstallFiles(object sender, EventArgs e) { string sourceDirectory = @"F:somepath"; string targetDirectory = directoryImput.Text; //Copy all the files & Replaces any files with the same name foreach (string newPath in System.IO.Directory.GetFiles(sourceDirectory, "*.*", SearchOption.AllDirectories)) File.Copy(newPath, newPath.Replace(sourceDirectory, targetDirectory), true); 
6
  • Yeah, I was using the above post as before this I was just moving files, however I am now in need of moving full folders. Commented Apr 25, 2017 at 11:19
  • what do you mean? You have another class and you want to execute method from that class? And that method takes parameter that user enters in textbox? Commented Apr 25, 2017 at 11:19
  • So, I have a form from1 and this has a text box where a user enter a vaule the text boxt is called Directoryinput. What I want to do is in a class call the value that is within Directoryinput as the targetdirectory. Commented Apr 25, 2017 at 11:21
  • Why do you write your application logic in main itself? A better approach would be moving the copy logic to a new method or helper function. Commented Apr 25, 2017 at 11:22
  • string targetDirectory = this.Directoryinput.Text; Commented Apr 25, 2017 at 11:25

1 Answer 1

1

I just realized that you are looking for a method to move all files and folders - silly me. Here, have some example from https://msdn.microsoft.com/en-us/library/bb762914.aspx :

using System; using System.IO; class DirectoryCopyExample { static void Main() { // Copy from the current directory, include subdirectories. DirectoryCopy(".", @".\temp", true); } private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs) { // Get the subdirectories for the specified directory. DirectoryInfo dir = new DirectoryInfo(sourceDirName); if (!dir.Exists) { throw new DirectoryNotFoundException( "Source directory does not exist or could not be found: " + sourceDirName); } DirectoryInfo[] dirs = dir.GetDirectories(); // If the destination directory doesn't exist, create it. if (!Directory.Exists(destDirName)) { Directory.CreateDirectory(destDirName); } // Get the files in the directory and copy them to the new location. FileInfo[] files = dir.GetFiles(); foreach (FileInfo file in files) { string temppath = Path.Combine(destDirName, file.Name); file.CopyTo(temppath, false); } // If copying subdirectories, copy them and their contents to new location. if (copySubDirs) { foreach (DirectoryInfo subdir in dirs) { string temppath = Path.Combine(destDirName, subdir.Name); DirectoryCopy(subdir.FullName, temppath, copySubDirs); } } } } 

Short version - place it in DirectoryManager.cs and call by DirectoryManager.CopyDirectory(source, destination):

using System.IO; class DirectoryManager { internal static void CopyDirectory(string input, string output) { DirectoryInfo dir = new DirectoryInfo(input); if (dir.Exists) { DirectoryInfo[] dirs = dir.GetDirectories(); Directory.CreateDirectory(output); FileInfo[] files = dir.GetFiles(); foreach (FileInfo file in files) { string temppath = Path.Combine(output, file.Name); file.CopyTo(temppath, false); } foreach (DirectoryInfo subdir in dirs) { string temppath = Path.Combine(output, subdir.Name); CopyDirectory(subdir.FullName, temppath); } } } } 
Sign up to request clarification or add additional context in comments.

4 Comments

thank you for that, I am still a little confused though haha. I am not sure how I would call the value that is entered in my text box.
I thought it might be like; System.IO.Directory.Move(sourceDirectory, targetDirectory);
System.IO.Directory.Move moves directory (files in sourceDirectory will vanish). The MSDN example is very good - but since it looks like you would prefer less clutter I'll edit my answer.
Thank you, looks like I am still having some issues haha

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.