c# - OpenFileDialog - How to prevent the default directory from being overwritten? -


i'm trying find way reset initial/default directory after closing openfiledialog. consider following example:

using (openfiledialog openfile = new openfiledialog()) {     // example: opens in 'desktop' directory     // user navigates 'documents' directory in form before selecting file     dialogresult result = openfile.showdialog();     if (result == dialogresult.ok) messagebox.show(openfile.filename); }  // somewhere else, code runs using (openfiledialog openfile = new openfiledialog()) {     // problem: opens in 'documents' directory. not good!     // how open using same default directory (ie: desktop)?     dialogresult result = openfile.showdialog();     if (result == dialogresult.ok) messagebox.show(openfile.filename); } 

just clear, 'desktop' example, won't know initial directory it's stored in registry (if understand correctly).

i tried using restoredirectory option. did not seem have effect. i've read elsewhere it's supposed reset environment.currentdirectory original value, sounds reasonable. however, don't think openfiledialog uses environment.currentdirectory since value never changed, , never matches openfiledialog opens (unless manually browse it).

is there might missing here? know how stop overwriting whichever directory variable openfiledialog uses default?

the default directory stored in following registry key on windows 7.

hkey_current_user\software\microsoft\windows\currentversion\explorer\comdlg32 

iirc, different on other os's, might want find out exact directory depending on os version.

so grab value when application launches, , save in memory, , set openfiledialog.initialdirectory every time before open dialog.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -