C# relative path not start from working directory

南楼画角 提交于 2019-12-01 20:53:31

It does start from the working directory. However, you shouldn't use the working directory, as it can vary if specific IO-Tasks (e.g. sometimes an Open File Dialog or (obviously) the Directory.SetCurrentDirectory method) are performed. Instead you should use the AppDomain.CurrentDomain.BaseDirectory property to get the path where the assembly file is located. You can use this like that:

var relativePath = Path.Combine ("report", "report1.rdlc");
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
var absolutePath = Path.Combine (baseDirectory, absolutePath);

Now you should work with the absolutePath to access the file.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!