因此,在我正在制作的程序中,我想在复制文件时写入文本文件。但是,我必须复制文件的代码是循环的。似乎当它写入文本文件时,它只在最后一个文件被复制时写入......不确定那里到底发生了什么,我希望我的描述有意义。这是一些代码......
//search through the source to find the matching file
foreach (var srcfile in Directory.GetFiles(sourceDir))
{
//cut off the source file from the source path same with destination
strSrcFile = srcfile.Split(Path.DirectorySeparatorChar).Last();
strDstFile = dstfile.Split(Path.DirectorySeparatorChar).Last();
//check the files before the move
CheckFiles(strSrcFile, strDstFile, srcfile, dstfile);
//if the destination and source files match up, replace the desination with the source
if (strSrcFile == strDstFile)
{
File.Copy(srcfile, dstfile, true);
//write to the text file
TextWriter writer = new StreamWriter(GlobalVars.strLogPath);
writer.WriteLine("Date: " + DateTime.Today + " Source Path: " + srcfile +
" Destination Path: " + dstfile + " File Copied: " + strDstFile + "\n\n");
//close the writer
writer.Close();
示例:假设我有一个源文件夹 X 将内容复制到文件夹 Y 并说文件夹 X 中的文件是 a.jpg、b.png、c.pdf
文本文件中发生了什么: 日期:8/8/2013 12:00:00 AM 源路径:C:\X\ 目标路径:C:\Y\ 文件复制:c.pdf
我想要发生的事情: 日期:8/8/2013 12:00:00 AM 源路径:C:\X\ 目标路径:C:\Y\ 文件复制:a.jpg 日期:8/8/2013 12: 00:00 AM 源路径:C:\X\ 目标路径:C:\Y\ 文件复制:b.png 日期:8/8/2013 12:00:00 AM 源路径:C:\X\ 目标路径: C:\Y\ 文件复制:c.pdf