我有一个像这样的设置类:
public class Settings
{
string resourcePath;
public string ResourcePath {
get {
return resourcePath + "/";
}
set {
resourcePath = value;
}
}
string texturePath;
public string TexturePath {
get {
string a = resourcePath + "/"; // This is just some debug stuff I did trying to find out wtf is going on
string b = texturePath + "/";
return a + b; // Breakpointing here shows that it is "Content/Textures/"
}
set {
texturePath = value;
}
}
public Settings ()
{
resourcePath = "Content";
texturePath = "Textures";
}
public static Settings CurrentSettings = new Settings();
}
然后我尝试从中获取TexturePath,如下所示:
string path = Settings.CurrentSettings.TexturePath + file;
属性返回的字符串是"Content//Content/Textures//"
我在这里缺少什么?为什么这样做?据我所知,它应该返回Content/Textures/
答案 0 :(得分:3)
使用Path.Combine处理路径。
string path = Path.Combine(Settings.CurrentSettings.TexturePath,file);
并且无需在您的媒体资源中添加“/”。
public string ResourcePath {
get {
return resourcePath;
}
set {
resourcePath = value;
}
}
答案 1 :(得分:2)
您可能无法平衡getter和setter之间的/
。你可能正在获得一些财产,然后用它来设置另一个 - 导致/
太多。
答案 2 :(得分:1)
您尚未显示产生您报告结果的代码,但以下代码非常可疑:
string resourcePath;
public string ResourcePath {
get {
return resourcePath + "/";
}
set {
resourcePath = value;
}
}
它总是在getter上附加一个正斜杠但从不在setter中删除它。所以下面的代码:
x.ResourcePath = "abc";
x.ResourcePath = x.ResourcePath + "/def";
x.ResourcePath = x.ResourcePath + "/ghi";
将ResourcePath
设置为“abc // def // ghi”。
我怀疑你遇到了类似的事情。