C#文件操作之把一个文件复制到另外一个文件夹下

一.文件复制例子如下,具体情况,根据需求扩展。

/// <summary>
///
/// </summary>
/// <param name="srcFolder">源目录</param>
/// <param name="destFolder">目标目录</param>
/// <param name="filename">源文件名</param>
public static string ImageCopty(string srcFolder, string destFolder, string filename)
{
DirectoryInfo directoryInfo = new DirectoryInfo(srcFolder);
FileInfo[] files = directoryInfo.GetFiles();
string imageName = "";
foreach (FileInfo file in files) // Directory.GetFiles(srcFolder)
{
if (file.Name == filename) //file.Extension == ".doc"
{
imageName = Guid.NewGuid().ToString("N") + file.Extension; //把原来的图片名字换成GUID
file.CopyTo(Path.Combine(destFolder,imageName));
break;
}
//file.MoveTo(Path.Combine(destFolder, file.Name));
}
return imageName;
}

上一篇:leetcode557


下一篇:c++中istringstream及ostringstream超详细说明