While running my project using Visual Studio, the working directory (AppDomain.CurrentDomain.BaseDirectory
) is {ProjectDirectoryPath}\bin\Debug\netcoreapp2.0
; but while running the project from its publish folder (after publishing it), the working directory is {PublishDirectoryPath}
. All project configuration and data files are located under {ProjectDirectoryPath}
and {PublishDirectoryPath}
in the same tree structure. What it means is that whenever I'm running the project using Visual Studio, I should prefix the path of each of the files with ../../../
.
My current solution is using the following function whenever accessing a file in the project directory:
public static string Root()
{
string root = AppDomain.CurrentDomain.BaseDirectory;
if (root.EndsWith("Debug\\netcoreapp2.0\\"))
root = root + Slash("../../..");
return root;
}
However, it feels to me that there must be some better way to solve this, since using files located under the project directory is nothing unusual (though it's more common to store configuration and data files under %AppData%
or /home
). So it doesn't seem right that so many developers would really implement a solution like this to something so common.
What am I missing?