加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 为什么路径会发生变化

发布时间:2020-12-15 19:36:39 所属栏目:百科 来源:网络整理
导读:我有代码,它以两种不同的形式检索一个目标路径.如果,在一种形式中,我选择一个路径来打开文件并处理它,当返回到另一个表单时,我收到一个Direcotry Exception错误.我习惯了不同的字符串来获得这条路径 在第二种形式中,我称之为: string strFilePath2; strFile
我有代码,它以两种不同的形式检索一个目标路径.如果,在一种形式中,我选择一个路径来打开文件并处理它,当返回到另一个表单时,我收到一个Direcotry Exception错误.我习惯了不同的字符串来获得这条路径

在第二种形式中,我称之为:

string strFilePath2;
       strFilePath2 = Directory.GetCurrentDirectory();
       strFilePath2 = Directory.GetParent(strFilePath2).ToString();
       strFilePath2 = Directory.GetParent(strFilePath2).ToString();
       strFilePath2 = strFilePath2 + "ACH";

我的第一个表格是:

strFilePath = Directory.GetCurrentDirectory();
       strFilePath = Directory.GetParent(strFilePath).ToString();
       strFilePath = Directory.GetParent(strFilePath).ToString();
       strFilePath = strFilePath + "ACH" + Node;

在调试过程中,我从第二种形式获取选定的路径,但不是我期望的路径.任何人都能说出原因吗?

解决方法

你检查了当前目录的值吗?

OpenFileDialog通常会更改当前目录.您可以使用RestoreDirectory属性控制该行为:

OpenFileDialog ofd = new OpenFileDialog();

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

顺便说一下,您在代码示例中连接路径.在.NET中,最好使用静态Path.Combine方法.此方法将检查是否存在反斜杠(或系统的路径分隔符),并在缺少时自动插入一个:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读