Mark Fenech
Mark Fenech

Reputation: 1358

Copying files from folder to another with C#

I am trying to copy files from a folder and paste them in another created folder

I already created the folder with the code below:

DirectoryInfo di = Directory.CreateDirectory(path);

where path is the path of where the folder is created.

How can I fill this folder with files from another folder pls.

Thanks

Upvotes: 0

Views: 135

Answers (2)

SomeRandomName
SomeRandomName

Reputation: 603

This will find and copy files with the specified search param.

public static void findAndCopy(string _sourcePath, string _destPath, string _searchParam )
{

    if (System.IO.Directory.Exists(_sourcePath))
    {
        string[] files = System.IO.Directory.GetFiles(_sourcePath, _searchParam, System.IO.SearchOption.AllDirectories);
        string destFile = "";
        string fileName = "";

        // Copy the files  
        foreach (string s in files)
        {
            // Use static Path methods to extract only the file name from the path.
            fileName = System.IO.Path.GetFileName(s);
            destFile = System.IO.Path.Combine(_destPath, fileName);
            try
            {
                System.IO.File.Copy(s, destFile, false);
            }
            catch (UnauthorizedAccessException uae)
            {
                log.Warn(uae);
            }
            catch (IOException ioe)
            {
                log.Warn(ioe);
            }
        }
    }
    else
    {
        log.Error("Source path not found! " + _sourcePath);
    }
}//end findAndCopy

Upvotes: 0

aaroncollett
aaroncollett

Reputation: 41

This should give you what you need:

http://msdn.microsoft.com/en-us/library/cc148994.aspx

Upvotes: 1

Related Questions