且构网

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

如何将所有文件从一个文件夹复制到另一个文件夹在C#

更新时间:2023-11-27 22:17:58

 的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.