./ changes target when i use OpenFileDialog
I am using streamwriter to log errors
the way it was designed (please don't ask why) is to open a new thread every time the app needs to log a message. It displays everything on. / Logs / [current-date] .txt which normally resolves to "c: \ myappfolder \ logs [current-date] .txt"
Everything works correctly, but after I use the open file dialog to say "C: \ home \ myfolder \ myfile" the streaming block tries to write to "c: \ home \ myfolder \ logs [current-date]. txt "
I know solutions to this problem, but I just don't understand what's going on
source to share
The current directory is the process scale value.
OpenFileDialog
changes the current directory.
If you are using the .NET class OpenFileDialog
, you can set the property to a RestoreDirectory
value true
to tell the dialog to leave the current directory alone (although the way to write documents for there RestoreDirectory
might be some threading issues, which I think might make this still irrelevant for the logger ).
source to share
As Mike B said, OpenFileDialog
can change the current directory. As ./
far as current is concerned, this also changes.
The property RestoreDirectory
changes this behavior.
Do something like this:
OpenFileDialog openFileDialog1 = new OpenFileDialog();
OpenFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
Taken from MSDN .
source to share
Mike B is absolutely correct.
If you are using a native API and not .NET, you need to set the OFN_NOCHANGEDIR option in the OPENFILENAME structure. The documentation states that it doesn't work for Windows XP, but not sure if this applies to the .NET version or not.
Regardless of how you fix it, keep in mind that every time the file dialog opens, it opens in the original folder. If you open a lot of files, the cure may be worse than the disease. You might be better off getting the current directory when you run the program and appending it to your filenames.
source to share