Dor*_*eka 3 c# desktop-application winforms
我有代码,它以两种不同的形式检索一个目标路径.如果,在一种形式中,我选择一个路径来打开文件并处理它,当返回到另一个表单时,我收到一个Direcotry Exception错误.我习惯了不同的字符串来获得这条路径
在第二种形式中,我称之为:
string strFilePath2;
strFilePath2 = Directory.GetCurrentDirectory();
strFilePath2 = Directory.GetParent(strFilePath2).ToString();
strFilePath2 = Directory.GetParent(strFilePath2).ToString();
strFilePath2 = strFilePath2 + "\\ACH";
Run Code Online (Sandbox Code Playgroud)
我的第一个表格是:
strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH\\" + Node;
Run Code Online (Sandbox Code Playgroud)
在调试过程中,我从第二种形式获取选定的路径,但不是我期望的路径.任何人都能说出原因吗?
你检查了当前目录的值吗?
在OpenFileDialog
通常将改变当前目录.您可以使用该RestoreDirectory
属性控制该行为:
OpenFileDialog ofd = new OpenFileDialog();
ofd.RestoreDirectory = true ; // this will not modify the current directory
Run Code Online (Sandbox Code Playgroud)
顺便说一下,您在代码示例中连接路径.在.NET中,最好使用静态Path.Combine
方法.此方法将检查是否存在反斜杠(或系统的路径分隔符),并在缺少时自动插入一个:
strFilePath = Path.Combine(strFilePath, "ACH");
Run Code Online (Sandbox Code Playgroud)