[solved] Using relative paths for ContentRootDirectory

I ran into some trouble when with the Content.Load<T>(string assetName) function, when trying to load files from directories given relative to the ContentRootDirectory.
Let’s say I have an *.xnb file for a SpriteFont, font.xnb, which I compiled with the MonoGame Content Pipeline. My ContentRootDirectory is found under “Debug(Release)/Content”. Using

SpriteFont font = Content.Load<SpriteFont>("font");

works fine. Also giving an absolute path, e.g.,

SpriteFont font = Content.Load<SpriteFont>(@"C:/assets/fonts/font");

results in loading of the file. However, when using relative path statements, such as

SpriteFont font = Content.Load<SpriteFont>(@"../../../fonts/font");

I get the message “Could not load …/…/…/fonts/font asset as a non-content file”. Interestingly, things like

SpriteFont font = Content.Load<SpriteFont>(@"../Content/font");

are working, but, again,

SpriteFont font = Content.Load<SpriteFont>(@"../../Debug/Content/font");

is not. I am rather confused about this behavior.

Are you sure all your paths are correct ? Does it change anything if you wrap them with System.IO.Path.GetFullPath() ? i.e.:

string absolutePath = System.IO.Path.GetFullPath(@"../../../fonts/font");
SpriteFont font = Content.Load<SpriteFont>(absolutePath );
1 Like

Thanks, that actually solves the problem! The paths were okay, but apparently, relative paths tend to cause problems in the loading routine. Why didn’t I think of that? :slight_smile: