A small and practical tool using C #

Source: Internet
Author: User

When I used this tool, I found the method of renaming a file in C # on the Internet and made one by myself. Although simple, it is quite practical and I can often use it

 

 

Just one fileCode:

Code

  Using  System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. text;
Using System. Windows. forms;
// C # Implementation of batch renaming file codes
// Add file operation space reference
Using System. IO;

Namespace Windowsapplication1
{
Public Partial Class Form1: Form
{
Public Form1 ()
{
Initializecomponent ();
}

Private Void Form1_load ( Object Sender, eventargs E)
{

}

Private Void Button#click ( Object Sender, eventargs E)
{
Folderbrowserdialog F1 = New Folderbrowserdialog ();
If (F1.showdialog () = Dialogresult. OK)
{
Textbox3.text = F1.selectedpath;
}


}

Private Void Button2_click ( Object Sender, eventargs E)
{
If (Textbox3.text ! = "" ){
If (Textbox1.text ! = "" )
{

String Stroldfilename;
String Strnewfilename;
String Stroldpart = This . Textbox1.text. Trim ();
String Strnewpart = This . Textbox2.text. Trim ();
String Strnewfilepath;
String Strfilefolder;
Int Totalfiles = 0 ;
Datetime starttime = Datetime. now; // Get Start Time
Try {
Directoryinfo di = New Directoryinfo (textbox3.text );
Fileinfo [] filelist = Di. getfiles ( " *.* " );
Strfilefolder = Textbox3.text;
Int I = 0 ;
Foreach (Fileinfo fi In Filelist)
{
Stroldfilename = Fi. Name;
Strnewfilename = Fi. Name. Replace (stroldpart, strnewpart );
Strnewfilepath = @ Strfilefolder + " \\ " + Strnewfilename;
Filelist [I]. moveTo (@ strnewfilepath );
Totalfiles + = 1 ;
This . Listbox1.items. Add ( " File Name: " + Stroldfilename + " Renamed " + Strnewfilename + "" );
I + = 1 ;

}
Datetime endtime = Datetime. now; // Get End Time
Timespan TS = Endtime - Starttime;
This . Listbox1.items. Add ( " Total time consumed: " + TS. Hours. tostring () + " Hour " + TS. Minutes. tostring () + " Minute " + TS. Seconds. tostring () + " Seconds " + TS. milliseconds. tostring () + " Millisecond " );
}
Catch
{
MessageBox. Show ( " The path is invalid! " );
}

}
Else
{
MessageBox. Show ( " No matching characters " );
}
}
Else
{
MessageBox. Show ( " Select a path first! " );
}
}
}
}

 

Related Article

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.