如何将所有文件从一个文件夹复制到另一个文件夹在C# [英] how to copy all files from a folder to another folder in c#
本文介绍了如何将所有文件从一个文件夹复制到另一个文件夹在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请让我知道如何将所有文件的文件夹中复制到另一个文件夹中的C#.NET。
目前我使用的:
诠释J = 1;
INT K = 1;
为(J = 1; J&小于5; J ++)
{
为(K = 1; K&γ-32; k ++)
{
字符串SOURCEPATH = @Desktop_location +\测试+ K +.LOG;
如果(System.IO.File.Exists(SOURCEPATH))
{
File.Copy(@Desktop_location +\\ \\统计服务器+ J +\测试+ K +.LOG,@Desktop_location +\\ \\统计转让\\测试+ J + K + .LOG);
//Console.WriteLine("Test结果);
}
其他
{
//Console.WriteLine("Test);
解决方案
的String []文件路径= Directory.GetFiles(@C:\ MYDIR \);
请参阅从目录获取文件
字符串mypath中= @C:\测试;
的foreach(在文件路径字符串的文件)
{
FileInfo的信息=新的FileInfo(文件);
如果(!File.Exists(info.FullName))
{
File.Copy(info.FullName,的新路径);
}
}
请参阅使用FileInfo类,你实际上并不需要它在这里,但它包含了很多有用的功能与文件和文件夹。 读它会帮助你规划你的应用程序。
Please let me know how to copy all files within a folder to another folder in c# .net.
Currently I am using :
int j = 1;
int k = 1;
for (j = 1; j < 5; j++)
{
for (k = 1; k < 32; k++)
{
string sourcePath = @Desktop_location + "\Test" + k + ".log";
if (System.IO.File.Exists(sourcePath))
{
File.Copy(@Desktop_location + "\\Statistics\\Server" + j + "\Test" + k + ".log", @Desktop_location + "\\Statistics\\Transfer\\test" + j + k + ".log");
//Console.WriteLine("Test Result");
}
else
{
//Console.WriteLine("Test");
解决方案
string[] filePaths = Directory.GetFiles(@"c:\MyDir\");
See Getting files from a directory
string myPath = @"C:\Test";
foreach (string file in filePaths)
{
FileInfo info = new FileInfo(file);
if (!File.Exists(info.FullName))
{
File.Copy(info.FullName, newPath);
}
}
See Using FileInfo Class, you don't actually require it here but it contains a lot of useful functionality for working with files and folders. Reading it will help you with planning your application.
这篇关于如何将所有文件从一个文件夹复制到另一个文件夹在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文