c# - How to "pre-select" a file in a SaveFileDialog -
often times in savefiledialog
find file user wants save selected, , have hit enter. create functionality in program.
this current attempt:
microsoft.win32.savefiledialog dlg = new microsoft.win32.savefiledialog(); dlg.filter = "cct files (.cct)|*.cct"; //filter .cct extensions nullable<bool> result = dlg.showdialog(); //show savefiledialog directoryname = directory.getcurrentdirectory(); //attempt directory has been opened filelist = directory.getfiles(@directoryname, "*.cct"); //put name of fill path file string form dlg.filename = filelist[0]; //set selecteditem previous file
i think problem whenever try getcurrentdirectory
, returns programs location (the debug folder), , not save location being opened up.
how allow preselect of file in location .cct
it's extension?
update clarity
i think clear things note savefiledialog
opens last directory saved file in program. directory want work with. it's happens when overwriting file you've been working on.
the key solve problem give, before opening savefiledialog, value initialdirectory
property.
of course, first time call savefiledialog there no previous record of choosen folder. point value known folder mydocuments
.
after first call choosen path , save in predefined setting of configuration file. when call made again retrieve value , apply initialdirectory
// default folder when no previous 1 has been saved... string directoryname = environment.getfolderpath(environment.specialfolder.mydocuments); microsoft.win32.savefiledialog dlg = new microsoft.win32.savefiledialog(); dlg.filter = "cct files (.cct)|*.cct"; //filter .cct extensions // try previous saved folder... if(configurationmanager.appsettings["workingdirectory"] != null) directoryname = configurationmanager.appsettings["workingdirectory"]; dlg.initialdirectory = directoryname; filelist = directory.getfiles(directoryname, "*.cct"); if(filelist.length > 0) { // set default name show in dialog dlg.filename = path.getfilename(filelist[0]); nullable<bool> result = dlg.showdialog(); if(result.hasvalue && result.value) { // try insert or update setting choosen path configuration config = configurationmanager.openexeconfiguration(configurationuserlevel.none); if(config.appsettings.settings["workingdirectory"] != null) config.appsettings.settings["workingdirectory"].value = path.getdirectoryname(dlg.filename); else config.appsettings.settings.add("workingdirectory",path.getdirectoryname(dlg.filename)); config.save(configurationsavemode.modified); configurationmanager.refreshsection("appsettings"); } }
Comments
Post a Comment