File 使用C将文件从文件夹复制到另一个文件夹#

File 使用C将文件从文件夹复制到另一个文件夹#,file,io,directory,File,Io,Directory,我正在尝试从一个文件夹复制文件并将其粘贴到另一个创建的文件夹中 我已经用下面的代码创建了文件夹: DirectoryInfo di = Directory.CreateDirectory(path); 其中路径是创建文件夹的路径 我如何用其他文件夹中的文件填充此文件夹 谢谢这应该能满足您的需求: 这将查找并复制具有指定搜索参数的文件 public static void findAndCopy(string _sourcePath, string _destPath, string _sear

我正在尝试从一个文件夹复制文件并将其粘贴到另一个创建的文件夹中

我已经用下面的代码创建了文件夹:

DirectoryInfo di = Directory.CreateDirectory(path);
其中路径是创建文件夹的路径

我如何用其他文件夹中的文件填充此文件夹


谢谢

这应该能满足您的需求:


这将查找并复制具有指定搜索参数的文件

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