2013-05-26 220 views
0

我想从一个文件夹中的文件复制并粘贴在其​​他创建的文件夹复制文件从文件夹复制到另一个用C#

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

DirectoryInfo di = Directory.CreateDirectory(path); 

其中path是创建文件夹的路径。

我怎样才能用这个文件夹填充另一个文件夹中的文件。

感谢

回答

0

这将找到并复制以指定的搜索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 
相关问题