C# 基础知识系列- 9 字符串的更多用法(二)
263
2022-08-07
C# copy files from source directory to destination file and rename repeated files and does not overr(曹海涛)
static void CopyFiles()
{
string sourceDir = @"D:\C\ll";
string destDir = @"D:\LL";
if (!Directory.Exists(destDir))
{
Directory.CreateDirectory(destDir);
}
string[] mp3Files= Directory.GetFiles(sourceDir, "*.mp3", SearchOption.AllDirectories);
if(mp3Files!=null && mp3Files.Any())
{
Dictionary
List
foreach (string mp3 in mp3Files)
{
string mp3FileName = Path.GetFileName(mp3);
string newMp3FileName = Path.GetFileName(mp3);
if (dic.ContainsKey(mp3FileName))
{
string guid = Guid.NewGuid().ToString().Substring(0, 6);
newMp3FileName = Path.GetFileNameWithoutExtension(mp3FileName) + guid + ".mp3";
dic.Add(newMp3FileName, newMp3FileName);
string repeatedMsg = $"mp3:{mp3},newMp3FileName:{newMp3FileName}";
repeatedList.Add(repeatedMsg);
}
else
{
dic.Add(mp3FileName, mp3FileName);
}
string newMp3FullName = Path.Combine(destDir, newMp3FileName);
File.Copy(mp3, newMp3FullName, false);
Console.WriteLine($"oldmp3:{mp3},newMp3FullName:{newMp3FullName}");
}
dic = null;
Console.WriteLine($"\n\n There are {repeatedList.Count} Repeated Msg:");
repeatedList.ForEach(x =>
{
Console.WriteLine(x);
});
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~