简体   繁体   中英

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 Environment.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 ?

  1. AppDomain.CurrentDomain.BaseDirectory
  2. Environment.CurrentDirectory
  3. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM