1

In my code I am trying to read a file using File.Open() with relative path. But some times the current directory is getting changed and directory not found exception is coming.

I am trying to figure out where is Environment.CurrentDirectory is getting changed. I checked the Directory.SetCurrentDirectory method but its calling to native method.

Is there any event which will notify me when Environment.CurrentDirectory has changed? How do I find out where exactly is directory changed?

I have tried putting break point in all occurrence of Environment.CurrentDirectory and Directory.SetCurrentDirectory().

vrnithinkumar
  • 1,273
  • 1
  • 11
  • 29

2 Answers2

1

No, but you could check if the current directory is correct before the File.Open() call, or, store the correct directory you want to use and use an absolute path for File.Open()

KinSlayerUY
  • 1,903
  • 17
  • 22
0

It's necessary more details, I've never seen this attribute changing during runtime.

  1. If you are running the executable from Start Menu ou another shortcuts, the current directory will change, check the property "Starts In".

    1. If you are deploying a Click Once application, every version was installed at different folder, and you won't find files saved at same executable folder.

    2. I found some references that OpenFileDialogs (and opendirectorydialog) changes the Environment.CurrentDirectory.

Source: OpenFileDialog and Environment.CurrentDirectory

Guergolet
  • 114
  • 1
  • 6