Cornel
Cornel

Reputation: 4709

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.

Upvotes: 2

Views: 6591

Answers (3)

Madi D.
Madi D.

Reputation: 1990

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.

Upvotes: 3

wj32
wj32

Reputation: 8403

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.

Upvotes: 1

Adriaan Stander
Adriaan Stander

Reputation: 166396

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.

Upvotes: 8

Related Questions