-1

I have bunch of text files in C:\Source folder. I want to copy all the files to MyData folder place across the C:\ drive. Please let me know the approach in C#, I am thinking it will be a recursive one.

I know how to copy the file from one location to another. I want to get the approach to get all the folders/Directories with name "MyData" across the C:. And the folder "MyData" is at multiple location. So I want to copy the files to all the places.

  • 4
    Please show your current efforts and code – Frederick Roth May 31 '13 at 13:26
  • 1
    @Frederick Roth - the OP does not sound like he knows where to start, no code required IMO to ask that kind of question. – killthrush May 31 '13 at 13:35
  • @Dev Dhingra - in case you're wondering why your question was downvoted, it's probably because it "does not show research effort". Others have asked (and answered) this question before, and it was very easy to find the link I provided using google. Something to think about for next time. – killthrush May 31 '13 at 13:55

3 Answers3

2

This answer is taken directly from MSDN here: http://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);
    DirectoryInfo[] dirs = dir.GetDirectories();

    if (!dir.Exists)
    {
        throw new DirectoryNotFoundException(
            "Source directory does not exist or could not be found: "
            + sourceDirName);
    }

    // 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);
        }
    }
}

}

Mike
  • 346
  • 2
  • 10
1

You can make use of FileSystemWatcher class in System.IO namespace.

public void FolderWatcher()
    {
        FileSystemWatcher Watcher = new System.IO.FileSystemWatcher();
        Watcher.Path = @"C:\Source";
        Watcher.Filter="*.txt";
        Watcher.NotifyFilter = NotifyFilters.LastAccess |
                     NotifyFilters.LastWrite |
                     NotifyFilters.FileName |
                     NotifyFilters.DirectoryName;
        Watcher.Created += new FileSystemEventHandler(Watcher_Created);
        Watcher.EnableRaisingEvents = true;

    }

    void Watcher_Created(object sender, FileSystemEventArgs e)
    {            
        File.Copy(e.FullPath,"C:\\MyData",true);            
    }
Bharath
  • 195
  • 1
  • 1
  • 19
0

If you really don't know where to start, I suggest taking a look at this question that was asked a while back. There are plenty of examples and links to get you started.

Community
  • 1
  • 1
killthrush
  • 4,859
  • 3
  • 35
  • 38