Hi all. I have code which retrieves a direcotry path in 2 different forms. If, in one form, I select a path to open a file and process it, when returning to the other form I am getting a Direcotry Exception error. I used to different strings to get that path
In the second form I called this:
string strFilePath2;
strFilePath2 = Directory.GetCurrentDirectory();
strFilePath2 = Directory.GetParent(strFilePath2).ToString();
strFilePath2 = Directory.GetParent(strFilePath2).ToString();
strFilePath2 = strFilePath2 + "\\ACH";
In my first form I called:
strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH\\" + Node;
During debugging, I am getting the selected path from the second form, but not the path I expected. Can any one tell why?
raghu vPosted Jun 3, 2011, 8:11 AM
The OpenFileDialog usually will change the current directory. You can control that behavior using the RestoreDirectory property:
OpenFileDialog ofd = new OpenFileDialog();
ofd.RestoreDirectory = true ; // this will not modify the current directory
As an aside, you are concatenating paths in your code sample. In .NET this is best done using the static Path.Combine method. This method will check for the presence of a backslash (or whatever the system's path separator character is) and automatically insert one if it is missing:
strFilePath = Path.Combine(strFilePath, "ACH");
ChaitanyaPosted Jun 3, 2011, 8:29 AM