c#将文件copy到另外一个路径中,如何批量循环copy
野鼻孔 2016-08-11 12:44:03 大神们,我要我向获取E:\\Interface\\下的所有文件,然后单个移动到C:\\Interface\\下, 我想把filename和filename2做为变量轮流读。
string filename = "YellowPage_CC_20160511.xls";
求帮忙改一下,如何获取文件名,并循环copy到C:\\Interface\\下,
谢谢,代码如下
string filepath = "E:\\Interface\\";
string[] filenames = Directory.GetFiles(filepath); //获取该文件夹下面的所有文件名
int i = 0;
if (i < filenames.Length)
{
textBox1.Text = filenames[i++];
Console.WriteLine(textBox1.Text);
}
string path1 = "E:\\Interface\\";
string filename = "YellowPage_CC_20160511.xls";
string sourcePath = path1 + filename;
//string filename = Convert.ToString(p[0]) + Convert.ToString(p[1]);
//string sourcePath = @"E:\Interface\YellowPage_CC_20160511.xls";
string path2 = "C:\\Interface\\";
string filename2 = "YellowPage_CC_20160511.xls";
string destPath = path2 + filename2;
FileInfo fi1 = new FileInfo(sourcePath);
FileInfo fi2 = new FileInfo(destPath);
try
{
// Create the file and clean up handles.
using (FileStream fs = fi1.Create()) { }
//Ensure that the target does not exist.
fi2.Delete();
//Copy the file.
fi1.CopyTo(destPath);
Console.WriteLine("{0} was copied to {1}.", sourcePath, destPath);
//Try to copy it again, which should succeed.
fi1.CopyTo(destPath, true);
Console.WriteLine("The second Copy operation succeeded, which is expected.");
}
catch
{
Console.WriteLine("Double copying was not allowed, which is not expected.");
}
}