我也遇到了这个确切的问题,我无法访问命名空间的 bin/debug 文件夹中的文件。我的解决方案是使用Split()
然后构造一个新字符串来操作字符串,该字符串是我在命名空间中拥有的 json 文件的绝对路径。
private static string GetFilePath()
{
const char Escape = '\\'; //can't have '\' by itself, it'll throw the "Newline in constant" error
string directory = Environment.CurrentDirectory;
string[] pathOccurences = directory.Split(Escape);
string pathToReturn = pathOccurences[0] + Escape; //prevents index out of bounds in upcoming loop
for(int i = 1; i < pathOccurences.Length; i++)
{
if (pathOccurences[i] != pathOccurences[i - 1]) //the project file name and the namespace file name are the same
pathToReturn += pathOccurences[i] + Escape;
else
pathToReturn += typeof(thisClass).Namespace + Escape; //In the one occurrence of the duplicate substring, I replace it with my class Namespace name
}
return pathToReturn + "yourFile.json";
}
我个人不喜欢这个解决方案,但这是我能想到的唯一答案。