在使用visualstudio运行我的项目时,工作目录(
AppDomain.CurrentDomain.BaseDirectory
)是
{ProjectDirectoryPath}\bin\Debug\netcoreapp2.0
; 但是在从发布文件夹运行项目时(发布之后),工作目录是
{PublishDirectoryPath}
{ProjectDirectoryPath}
和
{PublishDirectoryPath}
在同一个树结构中。这意味着每当我使用visualstudio运行项目时,我都应该在每个文件的路径前面加上前缀
../../../
.
我当前的解决方案是在访问项目目录中的文件时使用以下函数:
public static string Root()
{
string root = AppDomain.CurrentDomain.BaseDirectory;
if (root.EndsWith("Debug\\netcoreapp2.0\\"))
root = root + Slash("../../..");
return root;
}
但是,我觉得必须有更好的方法来解决这个问题,因为使用位于项目目录下的文件并不是什么稀奇的事情(尽管在项目目录下存储配置和数据文件更常见)
%AppData%
或
/home
). 所以,这么多开发人员真的要为如此常见的东西实现这样的解决方案,这似乎是不对的。
我错过了什么?