我有一个 C# 程序,它将从相对路径读取文件./report/report1.rdlc
,但是有时由于未知原因它从完全不同的位置找到了该文件C:\Windows\system32\report\report1.rdlc
但该文件实际上位于C:\Program Files (x86)\Application1\report\report1.rdlc
程序在里面C:\Program Files (x86)\Application1\
。相对路径不从工作目录开始有什么原因吗?
它确实从工作目录开始。但是,您不应该使用工作目录,因为如果特定的 IO 任务(例如,有时打开文件对话框或(显然)Directory.SetCurrentDirectory
方法)进行。相反,你应该使用AppDomain.CurrentDomain.BaseDirectory
属性来获取程序集文件所在的路径。你可以这样使用:
var relativePath = Path.Combine ("report", "report1.rdlc");
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
var absolutePath = Path.Combine (baseDirectory, absolutePath);
现在你应该与absolutePath
访问该文件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)