当前位置: 移动技术网 > IT编程>开发语言>c# > C# copy files from source directory to destination file and rename repeated files and does not override

C# copy files from source directory to destination file and rename repeated files and does not override

2019年11月21日  | 移动技术网IT编程  | 我要评论
  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);
                });
            }           
        }

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网