c# - Moving files from one location to another -
i'm trying make program move (cut , paste) files 1 directory (a folder) directory. in example, i'm trying move files inside d:\source folder (has few files in it) c:\source folder (which has no files in it). when run program, error.
http://s13.postimg.org/kuufg0gmu/error.jpg
here full source code:
using system.io; namespace filemanager { public partial class form1 : form { string sourcedirectory = ""; //string destinationdirectory = @"c:\destination"; string date = ""; string[] filepaths; string destinationpath; public form1() { initializecomponent(); } private void buttonclean_click(object sender, eventargs e) { // source directory sourcedirectory = textboxdirectory.text; // date of files date = textboxdate.text; // file paths if (directory.exists(sourcedirectory)) { filepaths = directory.getfiles(@sourcedirectory, "*", searchoption.alldirectories); foreach (string sourcepath in filepaths) { destinationpath = sourcepath.remove(0, 1).insert(0, "c"); file.copy(sourcepath, destinationpath); //messagebox.show(destinationpath); } } else { messagebox.show("directory not exist."); } } } }
you need check if destination directory exists copy files otherwise first create destination directory.
foreach (string sourcepath in filepaths) { destinationpath = sourcepath.remove(0, 1).insert(0, "c"); if(!directory.exists(destinationpath)) directory.createdirectory(destinationpath); file.copy(sourcepath, destinationpath); //messagebox.show(destinationpath); }
Comments
Post a Comment