我想将所有文件从当前文件夹复制到其中的另一个文件夹。 但不包括.exe和.dll文件。
这是我现在拥有的代码,它将每个文件从当前复制到另一个文件夹。我已经搜索了,并且根据扩展名没有几个要排除的答案,但这不是我想要的。我想根据其名称进行排除,以便以后可以排除更多文件。
string TargetDirectory = @"Copied";
if (Directory.Exists(TargetDirectory))
{
Directory.Delete(TargetDirectory, true);
}
Directory.CreateDirectory(TargetDirectory);
await Task.Delay(1000);
string SourceDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Logger.LogInfo("Creating Backup of the Source Directory...");
if (Directory.GetFiles(SourceDirectory).Length == 0)
{
Logger.LogError("No Files found in this directory. " + SourceDirectory);
return;
}
else
{
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(SourceDirectory, "*", SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(SourceDirectory, TargetDirectory));
//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(SourceDirectory, "*.*", SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(SourceDirectory, TargetDirectory), true);
}
答案 0 :(得分:0)
尝试,应该可以:
async Task<void> Copy(List<string> ExcludedNamesWithoutExtensions)
{
string TargetDirectory = @"Copied";
if (Directory.Exists(TargetDirectory))
{
Directory.Delete(TargetDirectory, true);
}
Directory.CreateDirectory(TargetDirectory);
await Task.Delay(1000);
string SourceDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Logger.LogInfo("Creating Backup of the Source Directory...");
if (Directory.GetFiles(SourceDirectory).Length == 0)
{
Logger.LogError("No Files found in this directory. " + SourceDirectory);
return;
}
else
{
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(SourceDirectory, "*", SearchOption.AllDirectories)) Directory.CreateDirectory(dirPath.Replace(SourceDirectory, TargetDirectory));
//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(SourceDirectory, "*.*", SearchOption.AllDirectories))
{
bool CanCopy = true;
string[] array = newPath.Split('\\')[newPath.Split('\\').Length - 1].Split('.');
string name = array[array.Length - 1];
foreach (string s in ExcludedNamesWithoutExtensions)
{
if (name != s)
{
CanCopy = false;
break;
}
}
if (CanCopy)
File.Copy(newPath, newPath.Replace(SourceDirectory, TargetDirectory), true);
}
}
}
答案 1 :(得分:0)
这应该有效:
HashSet<string> namesToExclude = new HashSet<string>()
{
"bla.dll",
"blubb.exe"
};
string TargetDirectory = @"Copied";
if (Directory.Exists(TargetDirectory))
{
Directory.Delete(TargetDirectory, true);
}
Directory.CreateDirectory(TargetDirectory);
await Task.Delay(1000);
string SourceDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Logger.LogInfo("Creating Backup of the Source Directory...");
if (Directory.GetFiles(SourceDirectory).Length == 0)
{
Logger.LogError("No Files found in this directory. " + SourceDirectory);
return;
}
else
{
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(SourceDirectory, "*", SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(SourceDirectory, TargetDirectory));
//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(SourceDirectory, "*.*", SearchOption.AllDirectories))
{
if (!namesToExclude.Contains(Path.GetFileName(newPath)))
{
File.Copy(newPath, newPath.Replace(SourceDirectory, TargetDirectory), true);
}
}
}
哦,要注意的是,如果根目录中只有目录而没有文件,则您的代码将不会复制,您可能还需要检查目录。子目录也将是一个问题。您可以使用递归来解决此问题。