VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之C# copy files from source directory to destination file and rename repeated files and does not overr

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

复制代码
  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<string, string> dic = new Dictionary<string, string>();
                List<string> repeatedList = new List<string>();
                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);
                });
            }           
        }
复制代码
相关教程