Tuesday, March 30, 2010

OpenFileDialog changes the current directory

I opened a file from my application and later on when I went to run a shell script that was in the application directory, it wasn't able to find the file.  It turns out that the OpenfileDialog changed the current directory and therefore it was looking for my shell script in the new location.  The fixes were:

1.  Change OpenFileDialog to use the RestoreDirectory option and set it to true.  Like this:
            OpenFileDialog fileDlg = new System.Windows.Forms.OpenFileDialog
            {
                Multiselect = MulitFileSelect,
                Filter = "Data Sources (*.txt, *.csv|*.txt*;*.csv|All Files|*.*",
                RestoreDirectory = true
            };
2.   The next thing I did was when I looked for the Shell Script, if it didn't find the file, I inserted code to go to the Application.ExecutablePath and try there.  Keep in mind that ExecutablePath returns the .exe name so you need to call Path.GetDirectoryname on it if you want to get the directory location.


No comments: