c#复制文件

    xiaoxiao2021-03-26  20

    private bool CopyFolder(string from, string to)         {             try             {                 if (!Directory.Exists(to))                     Directory.CreateDirectory(to);                 if (File.Exists(from))                 {                     FileInfo f = new FileInfo(from);                     f.CopyTo(to + "\\" + f.Name, true);                     // 是文件                 }                 else if (Directory.Exists(from))                 {                     // 子文件夹                     foreach (string sub in Directory.GetDirectories(from))                         CopyFolder(sub + "\\", to + "\\" + Path.GetFileName(sub) + "\\");                     // 文件                     foreach (string file in Directory.GetFiles(from))                     {                         if (Path.GetFileName(file).IndexOf("log") < 0 && Path.GetFileName(file).IndexOf("deploy") < 0)                             //File.Copy(file, to + "\\" + Path.GetFileName(file), true);//可以覆盖                         File.Copy(file, Path.Combine(to ,Path.GetFileName(file)), true);//可以覆盖                                              }                     // 是文件夹                 }                 else                 {                     // MessageBox.Show(from + "不是有效文件或路径!");                     return false;                 }             }             catch (Exception ex)             {                 MessageBox.Show(ex.ToString());                 return false;             }             return true;         } File.Copy(path1, Path.Combine(path2, Path.GetFileName(path1), true)//,这就可以覆盖掉同名文件了 File.Copy(file, Path.Combine(to ,Path.GetFileName(file)), true);//这个成功,但是to不包含文件名称
    转载请注明原文地址: https://ju.6miu.com/read-661193.html

    最新回复(0)