C # cross-drive characters move files

Source: Internet
Author: User

Write mobile files when the practice found that directory.move can only be in the same drive to operate, so the Niang, but also really someone has encountered this situation, search for a man wrote a cross-letter mobile demo is very good to share, the original is as follows:

1  Public voidMoveDirectory (stringStrsourcedir,stringStrdestdir,BOOLBdelsource)2         {3             if(Directory.Exists (strsourcedir))4             {5                 if(Directory.getdirectoryroot (strsourcedir) = =Directory.getdirectoryroot (strdestdir))6                 {7 Directory.move (Strsourcedir, strdestdir);8                 }9                 ElseTen                 { One                     Try A                     { -CopyDirectory (NewDirectoryInfo (Strsourcedir),NewDirectoryInfo (Strdestdir)); -                         if(Bdelsource) Directory.delete (Strsourcedir,true); the                     } -                     Catch(Exception Subex) -                     { -                         ThrowSubex; +                     } -                 } +             } A         } at         Private voidcopydirectory (DirectoryInfo disourcedir, DirectoryInfo didestdir) -         { -             if(!didestdir.exists) didestdir.create (); -fileinfo[] Fisrcfiles =disourcedir.getfiles (); -             foreach(FileInfo Fisrcfileinchfisrcfiles) -             { in Fisrcfile.copyto (Path.Combine (Didestdir.fullname, Fisrcfile.name)); -             } todirectoryinfo[] Disrcdirectories =disourcedir.getdirectories (); +             foreach(DirectoryInfo disrcdirectoryinchdisrcdirectories) -             { theCopyDirectory (Disrcdirectory,NewDirectoryInfo (Path.Combine (didestdir.fullname, Disrcdirectory.name )); *             } $         }Panax Notoginseng         Private voidButton1_Click (Objectsender, EventArgs e) -         { the             //MoveFolder1 (); +             stringSnowfile = System.Windows.Forms.Application.StartupPath +"\\seo"; A             stringSnfile = Environment.SystemDirectory.Replace ("System32",""); theMoveDirectory (Snowfile, Snfile,true); +}

Simple code to make people suddenly enlightened, in fact, we can change the angle, he is to create a path to traverse the source address file copied to the destination address, and then delete the original file. This solves, the code is easy to understand!

C # cross-drive characters move files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.