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

c# – 为什么路径会发生变化,第1张

概述我有代码,它以两种不同的形式检索一个目标路径.如果,在一种形式中,我选择一个路径来打开文件并处理它,当返回到另一个表单时,我收到一个Direcotry Exception错误.我习惯了不同的字符串来获得这条路径 在第二种形式中,我称之为: string strFilePath2; strFilePath2 = Directory.GetCurrentDirectory(); 我有代码,它以两种不同的形式检索一个目标路径.如果,在一种形式中,我选择一个路径来打开文件并处理它,当返回到另一个表单时,我收到一个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");
总结

以上是内存溢出为你收集整理的c# – 为什么路径会发生变化全部内容,希望文章能够帮你解决c# – 为什么路径会发生变化所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1224491.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存