c# - How to get file path from OpenFileDialog and FolderBrowserDialog? -
hey there started learning c# few days ago , i'm trying make program copies , pastes files (and replaces if needed) selected directory don't know how directory , file paths openfiledialog , folderbrowserdialog
what doing wrong?
here's code:
namespace filereplacer { public partial class form1 : form { public form1() { initializecomponent(); } private void direc_click(object sender, eventargs e) { string folderpath = ""; folderbrowserdialog directchoosedlg = new folderbrowserdialog(); if (directchoosedlg.showdialog() == dialogresult.ok) { folderpath = directchoosedlg.selectedpath; } } private void choof_click(object sender, eventargs e) { openfiledialog choofdlog = new openfiledialog(); choofdlog.filter = "all files (*.*)|*.*"; choofdlog.filterindex = 1; choofdlog.multiselect = true; choofdlog.showdialog(); } private void replacebtn_click(object sender, eventargs e) { // i'm having trouble } public static void replacefile(string filetomoveanddelete, string filetoreplace, string backupoffiletoreplace) { file.replace(filetomoveanddelete, filetoreplace, backupoffiletoreplace, false); } }
for openfiledialog:
openfiledialog choofdlog = new openfiledialog(); choofdlog.filter = "all files (*.*)|*.*"; choofdlog.filterindex = 1; choofdlog.multiselect = true; if (choofdlog.showdialog() == dialogresult.ok) { string sfilename = choofdlog.filename; string[] arrallfiles = choofdlog.filenames; //used when multiselect = true }
for folderbrowserdialog:
folderbrowserdialog fbd = new folderbrowserdialog(); fbd.description = "custom description"; if (fbd.showdialog() == dialogresult.ok) { string sselectedpath = fbd.selectedpath; }
to access selected folder
, selected file name
can declare both string @ class level.
namespace filereplacer { public partial class form1 : form { string sselectedfile; string sselectedfolder; public form1() { initializecomponent(); } private void direc_click(object sender, eventargs e) { folderbrowserdialog fbd = new folderbrowserdialog(); //fbd.description = "custom description"; //not mandatory if (fbd.showdialog() == dialogresult.ok) sselectedfolder = fbd.selectedpath; else sselectedfolder = string.empty; } private void choof_click(object sender, eventargs e) { openfiledialog choofdlog = new openfiledialog(); choofdlog.filter = "all files (*.*)|*.*"; choofdlog.filterindex = 1; choofdlog.multiselect = true; if (choofdlog.showdialog() == dialogresult.ok) sselectedfile = choofdlog.filename; else sselectedfile = string.empty; } private void replacebtn_click(object sender, eventargs e) { if(sselectedfolder != string.empty && sselectedfile != string.empty) { //use selected folder path , file path } } .... }
note:
as have kept choofdlog.multiselect=true;
, means in openfiledialog()
able select multiple files (by pressing ctrl
key , left mouse click selection).
in case selected files in string[]
:
at class level:
string[] arrallfiles;
locate line (when multiselect=true
line gives first file only):
sselectedfile = choofdlog.filename;
to files use this:
arrallfiles = choofdlog.filenames; //this line gives array of selected files
Comments
Post a Comment