Search code examples
.netc++relative-pathopenfiledialog

./ changes target when i use OpenFileDialog


I'm using a streamwriter to log errors

the way it has been designed (please dont ask why) is to open a new streamwriter everytime the application has to log a message. It outputs everything to ./Logs/[current-date].txt which usually resolves to "c:\myappfolder\logs[current-date].txt"

Everything works correctly, but after I use an open file dialog to say, "C:\home\myfolder\myfile" the streamwriter tries to write to "c:\home\myfolder\logs[current-date].txt"

I know solutions to this problem but i just dont understand what's going on


Solution

  • The current directory is a process wide value.

    The OpenFileDialog is changing the current directory.

    If you're using the .NET OpenFileDialog class, you can set the RestoreDirectory property to true to tell the dialog to leave the current directory alone (although the way the docs for RestoreDirectory is written there may be some threading issues, which I imagine might make this still inappropriate for a logging facility).