基本上,我想复制一个文件夹中除一个文件夹之外的所有内容,该文件夹恰好是我的程序存储日志的位置。(我知道我可以将日志存储在其他地方,但我有我的理由)。到目前为止我有:
private void btnCopyFiles_Click(object sender, EventArgs e)
{
try
{
//Copy all the files
foreach (string newPath in Directory.GetFiles(@"\\xxx\yyy", "*.*",
SearchOption.AllDirectories)
.Where(p => p != Logging.fullLoggingPath))
File.Copy(newPath, newPath.Replace(@"\\xxx\yyy", @"C:\bbb"));
using (StreamWriter w = File.AppendText(Logging.fullLoggingPath))
{
Logging.Log("All necessary files copied to C:\\bbb", w);
}
}
catch
{
using (StreamWriter w = File.AppendText(Logging.fullLoggingPath))
{
Logging.Log("Error copying files, make sure you have permissions to access the drive and write to the folder.", w);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我如何修改它以排除其中的一个特定文件夹\\xxx\yyy
在循环中,只需添加一个条件即可排除该目录
If (Path.GetDirectory(newPath) == PathToExclude)
Continue;
Run Code Online (Sandbox Code Playgroud)