C # file rename

Source: Internet
Author: User

 

C #, when renaming a file, noRenameThis function uses the fileinfo. moveTo method, and transfers moveTo to a new name in the original directory, that is, rename.

This method is used to rename the names of all subfolders in the folder. The names of the new subfolders can use format strings, such as dir {0: 0000}. The renamed subfolders are as follows: dir0001, dir0002, and dir0003.

 

For more information, see the example.Code.

 

  1. /// <Summary>
  2. ///Rename all subfolders in the folder
  3. /// </Summary>
  4. /// <Param name = "directoryname">Folder name</Param>
  5. /// <Param name = "newdirectoryname">New subfolder name Format String</Param>
  6. Public VoidRenamedirectories (StringDirectoryname,StringNewdirectoryname)
  7. {
  8. IntI = 1;
  9. String[] Sdirectories = directory. getdirectories (directoryname );
  10. Foreach(StringSdirectoryInSdirectories)
  11. {
  12. StringSdirectoryname = path. getfilename (sdirectory );
  13. StringSnewdirectoryname =String. Format (newdirectoryname, I ++ );
  14. StringSnewdirectory = path. Combine (directoryname, snewdirectoryname );
  15. Directory. Move (sdirectory, snewdirectory );
  16. }
  17. }

 

 

// Full path name of the original file in the list
String oldstr = ft. fileroute + @ "/" + ft. filename + ft. exname;

// New file name
String newstr = ft. fileroute + @ "/" + prechar + name + nextchar + ft. exname;

// Rename Method
Fileinfo Fi = new fileinfo (oldstr );
Fi. moveTo (path. Combine (newstr ));

 

 

Both of them are supported.

 

 

 

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.