c# savefiledialog lock to particular directory? -
this question has answer here:
- c# savefiledialog in specific folder 3 answers
how save files particular directory using savefiledialog in c#. path user should not change directory when savefile dialog opened.
there no direct way (a property keepsamefolder=true
example).
option write event handler fileok event , cancel click on save button if folder not 1 like.
cancelling event doesnt't close savefiledialog , user fix error
// declare @ global class level string allowedpath = @"c:\temp"; private void openfiledialog1_fileok(object sender, canceleventargs e) { if (!path.getdirectoryname(openfiledialog1.filename) == allowedpath ) { messagebox.show("you should save file in or sub folder of: " + allowedpath); e.cancel = true; } }
edit: following comment of mr hofman, if allow subfolder of base path choosen should change check inside event handler like
string userchoice = path.getdirectoryname(openfiledialog1.filename); if (!userchoice.startswith(allowedpath)) { messagebox.show("you should save file in folder: " + allowedpath); e.cancel = true; }
however, said in comments above, think using savefiledialog when user has no option on he/she wants save file not choice. if folder predefined prepare reusable inputform asks file name , build full filename in code.
Comments
Post a Comment