当前位置: 移动技术网 > IT编程>开发语言>c# > C# copy source directory files with original folder to the destination path

C# copy source directory files with original folder to the destination path

2019年12月04日  | 移动技术网IT编程  | 我要评论
private static void PathCopyFilesWithOriginalFolder() { int sourceFilesNum = 0; try { string sourceDir = @"E:\Source"; string destDir = @"E:\Dest"; st... ...

 

private static void pathcopyfileswithoriginalfolder()
        {
            int sourcefilesnum = 0;
            try
            {                
                string sourcedir = @"e:\source";
                string destdir = @"e:\dest";
                string[] allsourcefiles = directory.getfiles(sourcedir, "*", searchoption.alldirectories);
                if (allsourcefiles != null && allsourcefiles.any())
                {
                    foreach (var sourcefilefullname in allsourcefiles)
                    {
                        string sourcefiledir = path.getdirectoryname(sourcefilefullname);
                        string sourcefilerelativedir = string.empty;
                        if (sourcefiledir.length > sourcedir.length)
                        {
                            sourcefilerelativedir = sourcefiledir.substring(sourcedir.length + 1);
                        }
                        else
                        {
                            sourcefilerelativedir = "";
                        }
                        string destfiledir = path.combine(destdir, sourcefilerelativedir);
                        if (!directory.exists(destfiledir))
                        {
                            directory.createdirectory(destfiledir);
                        }

                        string destfilefullname = path.combine(destfiledir, path.getfilename(sourcefilefullname));
                        file.copy(sourcefilefullname, destfilefullname, true);
                        string msg = $"sourcefilefullname:{sourcefilefullname},destfilefullname:{destfilefullname}";
                        console.writeline(msg);
                        sourcefilesnum++;
                    }
                }
            }
            catch(exception ex)
            {
                messagebox.show(ex.message);
            }
            finally
            {
                system.diagnostics.debug.writeline(sourcefilesnum);
            }            
        }

 

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网