gpt4 book ai didi

c# - 从c#中的文件夹复制所有类型的格式文件

转载 作者:太空狗 更新时间:2023-10-30 00:05:32 25 4
gpt4 key购买 nike

我正在尝试将所有格式文件(.txt、.pdf、.doc ...)文件从源文件夹复制到目标。

我只为文本文件编写代码。

复制所有格式文件应该怎么做?

我的代码:

string fileName = "test.txt";
string sourcePath = @"E:\test222";
string targetPath = @"E:\TestFolder";

string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);

复制文件的代码:

System.IO.File.Copy(sourceFile, destFile, true);

最佳答案

使用 Directory.GetFiles 并循环路径

string sourcePath = @"E:\test222";
string targetPath = @"E:\TestFolder";

foreach (var sourceFilePath in Directory.GetFiles(sourcePath))
{
string fileName = Path.GetFileName(sourceFilePath);
string destinationFilePath = Path.Combine(targetPath, fileName);

System.IO.File.Copy(sourceFilePath, destinationFilePath , true);
}

关于c# - 从c#中的文件夹复制所有类型的格式文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10927782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com