且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从一个路径复制到另一个文件路径在C#

更新时间:2023-11-27 16:31:34

我觉得你是System.IO命名空间之后。使用File.Copy可以提供解决方案。 而Directory.Exists /创建可以使目录不存在。

I think you are after the System.IO namespace. Using File.Copy can provide the solution. And Directory.Exists / create can make the directory is not existing.

var fileName = "tmp.txt";
var from = @"c:\temp\" + fileName;
var to = @"c:\temp\1\";
if (!Directory.Exists(to))
    Directory.CreateDirectory(to);

File.Copy(from, to + fileName);

您可以去的FileInfo以及。 (另外,在System.IO命名空间)

You can go for FileInfo aswell. (Also in the System.IO namespace)

var file = new FileInfo(@"c:\temp\tmp.txt");
var to = @"c:\temp\1\";
if (!Directory.Exists(to))
        Directory.CreateDirectory(to);

file.CopyTo(to + file.Name);