OpenFileDialog and Environment.CurrentDirectory
After using the OpenFileDialog
for selecting a file the Environment.CurrentDirectory
changes to the folder of the file. Is this normal?
From my understanding Envir开发者_JAVA百科onment.CurrentDirectory should always be the application folder.
Have a look at
Difference of AppDomain.CurrentDomain.BaseDirectory and Environment.CurrentDirectory
What is the difference of the follows ?
- AppDomain.CurrentDomain.BaseDirectory
- Environment.CurrentDirectory
- AppDomainSetup.ApplicationBase
1 and 3 are basically thr same. The difference is that AppDomainSetup.ApplicationBase is writable, but AppDomain.BaseDirectory is read-only since you can't change it after the appdomain has been created.
2 is something entirely different. It's used to resolve relative paths, among other things. You can change the CurrentDirectory at any time in your code, and it may also be changed by things like the FileDialogs.
Because the dialog box changes the Environment.CurrentDirectory while searching for files or folders .. or selecting them for upload..etc..
Note: as indicated in the comment to you question, following the link to the duplicate question.. use
FileDialog.RestoreDirectory property.
Yes it's normal, and no the current directory is not always the application folder. Run cmd and use the cd command to change the directory. You've just changed the same value that appears in Environment.CurrentDirectory! You should only ever be using the current directory value when the user specifies a file name through the command line or a similar mechanism. If you want the application folder use the Application.StartupPath property.
精彩评论