C # WinForm File bulk transcoding Select folder open specified directory
Private voidBtnfile_click (Objectsender, EventArgs e) {OpenFileDialog FileDialog=NewOpenFileDialog (); Filedialog.multiselect=true; Filedialog.title="Please select a file"; Filedialog.filter="All Files (*. *) |*.*";if(Filedialog.showdialog () = =DialogResult.OK) {stringfile=Filedialog.filename; MessageBox.Show ("file selected:"+ File,"Select File Hints", messageboxbuttons.ok,messageboxicon.information);}}Private voidBtnpath_click (Objectsender, EventArgs e) {FolderBrowserDialog Dialog=NewFolderBrowserDialog ();d ialog. Description="Please select a file path";if(dialog. ShowDialog () = =DialogResult.OK) {stringFoldpath =dialog. SelectedPath; MessageBox.Show ("folder selected:"+ Foldpath,"Select folder Hints", MessageBoxButtons.OK, messageboxicon.information);}}Private voidBtnopen_click (Objectsender, EventArgs e) {System.Diagnostics.Process.Start ("Explorer.exe","C:\\Windows");}
View Code
Batch file transcoding
#regionFile Bulk transcoding#regionCollection of file suffix names//file suffix name Privatelist<string>_fileextension; /// <summary> ///file suffix name/// </summary> Publiclist<string>FileExtension {Get{return_fileextension;} Set{_fileextension =value;} } #endregion /// <summary> ///File Bulk transcoding/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidBtnchangeencoding_click (Objectsender, EventArgs e) { Try { stringStrextension = This. TxtExtension.Text.Trim (); if(!string. Isnullorwhitespace (strextension)) {fileextension= Strextension.tolower (). Split (New Char[] {',',','},stringsplitoptions.removeemptyentries). ToList (); } Else{MessageBox.Show ("Enter the file suffix name!"); return; } stringsourceDirName = This. TxtSourceFoldPath.Text.Trim (); if(string. Isnullorwhitespace (sourceDirName)) {MessageBox.Show ("Please select the source folder!"); return; } stringdestDirName = This. TxtDestFoldPath.Text.Trim (); if(string. Isnullorwhitespace (destDirName)) {MessageBox.Show ("Please select a destination folder!"); return; } copydirectory (sourceDirName, destdirname); MessageBox.Show ("The conversion was successful!"); } Catch(Exception ex) {MessageBox.Show ("conversion failed!"+Ex. ToString ()); Throw; } } #regionTranscode file Encoding/// <summary> ///File Conversion Encoding/// </summary> /// <param name= "SourceFile" >source File</param> /// <param name= "DestFile" >target file</param> /// <param name= "targetencoding" >Coding</param> Private voidConvertfileencoding (stringSourceFile,stringdestfile, Encoding targetencoding) {DestFile=string. Isnullorwhitespace (destfile)?Sourcefile:destfile; stringStrsource =System.IO.File.ReadAllText (sourcefile, System.Text.Encoding.Default); System.IO.StreamWriter SW=NewSystem.IO.StreamWriter (DestFile,false, targetencoding); Sw. WriteLine (strsource); Sw. Close (); } /// <summary> ///Copy Folder/// </summary> /// <param name= "sourceDirName" >source File</param> /// <param name= "destDirName" >target file</param> Private voidCopyDirectory (stringsourceDirName,stringdestdirname) { Try { if(!directory.exists (destDirName)) {directory.createdirectory (destdirname); System.IO.File.SetAttributes (destDirName, System.IO.File.GetAttributes (sourceDirName)); } if(Destdirname[destdirname.length-1]! = Path.directoryseparatorchar)// =="\\"{destdirname= destDirName +Path.directoryseparatorchar; } string[] files =Directory.GetFiles (sourceDirName); foreach(stringFileinchfiles) { if(System.IO.File.Exists (destDirName +path.getfilename (file))) Continue; //transcoding replication if(Fileextension.contains (path.getextension (file). ToLower ())) {convertfileencoding (file, destDirName+path.getfilename (file), System.Text.Encoding.UTF8); System.IO.File.SetAttributes (destDirName+path.getfilename (file), fileattributes.normal); //total++; } //System.IO.File.Copy (file, destDirName + path.getfilename (file), true); //System.IO.File.SetAttributes (destDirName + path.getfilename (File), fileattributes.normal); ////total++; } string[] dirs = Directory.getdirectories (sourcedirname);//include path foreach(stringDirinchdirs) {CopyDirectory (dir, destDirName+Path.getfilename (dir)); } } Catch(Exception ex) {StreamWriter SW=NewStreamWriter (AppDomain.CurrentDomain.BaseDirectory +"\\log.txt",true);//System.Web.Providers.EntitiesSw. Write (ex. Message +" "+ DateTime.Now +"\ r \ n"); Sw. Close (); Throw; } } /// <summary> ///destination Folder/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidBtndestfolder_click (Objectsender, EventArgs e) {FolderBrowserDialog Fbdlgsource=NewFolderBrowserDialog (); if(Fbdlgsource.showdialog () = =DialogResult.OK) { This. Txtdestfoldpath.text =Fbdlgsource.selectedpath; } } /// <summary> ///source Folder/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidBtnsourcefolder_click (Objectsender, EventArgs e) {FolderBrowserDialog Fbdlgsource=NewFolderBrowserDialog (); if(Fbdlgsource.showdialog () = =DialogResult.OK) { This. Txtsourcefoldpath.text =Fbdlgsource.selectedpath; } } #endregion #endregion
View Code
C # WinForm File bulk transcoding Select folder