We often encounter problems such as copying and uploading files. Especially in automatic production, the files thrown by the machine need to be transported, collected, and then analyzed in the data, and so on.
WinForm, the collection of more common file handling.
1 Private voidMoveFile ()2 {3 stringFrompath =@"D:\Test\OutPut";4 stringDirectoryPath =@"D:\report";5 Try6 {7 string[] piclist = Directory.GetFiles (Frompath,"*.jpg");//Image8 string[] txtlist = Directory.GetFiles (Frompath,"*.txt");//text File9 string[] pdflist = Directory.GetFiles (Frompath,"*.pdf");//PDF FileTen One foreach(stringFinchpiclist) A { - //gets the file name. - stringFName = f.substring (Frompath.length +1); theFile.Copy (Path.Combine (Frompath, FName), Path.Combine (DirectoryPath, FName),true); - } - foreach(stringFinchtxtlist) - { + stringFName = f.substring (Frompath.length +1); - Try + { A file.copy (Path.Combine (Frompath, FName), Path.Combine (DirectoryPath, FName)); at } - //catches an exception. - Catch(IOException copyerror) - { - MessageBox.Show (copyerror.message); - } in } - foreach(stringFinchpdflist) to { + stringFName = f.substring (Frompath.length +1); - Try the { * file.copy (System.IO.Path.Combine (Frompath, FName), System.IO.Path.Combine (DirectoryPath, FName )); $ }Panax Notoginseng Catch(IOException copyerror) - { the MessageBox.Show (copyerror.message); + return; A } the } + //Delete files in the original folder - foreach(stringFinchtxtlist) $ { $ File.delete (f); - } - foreach(stringFinchpiclist) the { - File.delete (f);Wuyi } the foreach(stringFinchpdflist) - { Wu File.delete (f); - } About $ } - Catch(directorynotfoundexception dirnotfound) - { - MessageBox.Show (dirnotfound.message); A } +}
C # File handling (copy from one folder to another folder)