C# Copy一个文件到另一个文件夹下

 1  public static void CopyToFile()
 2         {
 3             //源文件路径
 4             string sourceName = @"D:\Source\Test.txt";
 5 
 6             //目标路径:项目下的NewTest文件夹,(如果没有就创建该文件夹)
 7             string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NewTest");
 8             if (!Directory.Exists(folderPath))
 9             {
10                 Directory.CreateDirectory(folderPath);
11             }
12 
13             //文件不用新的文件名,就用原文件文件名
14             string fileName = Path.GetFileName(sourceName);
15             ////可以选择给文件换个新名字
16             //string fileName = string.Format("{0}.{1}", "newFileText", "txt");
17 
18             //目标整体路径
19             string targetPath = Path.Combine(folderPath, fileName);
20 
21             //Copy到新文件下
22             FileInfo file = new FileInfo(sourceName);
23             if (file.Exists)
24             {
25                 //true 覆盖已存在的同名文件,false不覆盖
26                 file.CopyTo(targetPath, true);
27             }
28         }

 

posted @ 2019-04-02 14:28  shzhq  阅读(3192)  评论(0编辑  收藏  举报