Why is the path changing

I have code that returns a directcotry path in 2 different forms. If in one form I choose the path to open the file and process it, then when I return to another form, I get a Direcotry exception error. I used different lines to get this way

In the second form, I called it:

       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 get the selected path from the second form, but not the path I was expecting. Can anyone explain why?

+3
source share
3 answers

Have you checked the value of the current directory?

OpenFileDialogusually changes the current directory. You can control this behavior using the property RestoreDirectory:

OpenFileDialog ofd = new OpenFileDialog();

ofd.RestoreDirectory = true ; // this will not modify the current directory

, . .NET , Path.Combine. ( ) , :

strFilePath = Path.Combine(strFilePath, "ACH");
+8

FolderBrowserDialog, a OpenFileDialog - . ( ) .

- , - .

+3

OpenFileDialog SaveFileDialog , . reset , .RestoreDirectory = true;, . FolderBrowserDialog, , .

+3

Source: https://habr.com/ru/post/1755689/


All Articles