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);
Tom
  • 12,928
  • 2
  • 15
  • 31
  • 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. – Tom Apr 25 '17 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? – Nino Apr 25 '17 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. – Tom Apr 25 '17 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. – Randi Ratnayake Apr 25 '17 at 11:22
  • `string targetDirectory = this.Directoryinput.Text;` – Nino Apr 25 '17 at 11:25
  • You should post your own solution as answer - and it will not work for any files without '.' in filename. – Hekkaryk Apr 25 '17 at 12:21

1 Answers1

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);
            }
        }
    }
}
Hekkaryk
  • 522
  • 5
  • 13
  • 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. – Tom Apr 25 '17 at 11:43
  • I thought it might be like; `System.IO.Directory.Move(sourceDirectory, targetDirectory);` – Tom Apr 25 '17 at 11:55
  • 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. – Hekkaryk Apr 25 '17 at 12:23
  • Thank you, looks like I am still having some issues haha – Tom Apr 25 '17 at 12:35