来自相对路径的 C# PrivateFontCollection AddFontFile

C# PrivateFontCollection AddFontFile from relative path

我在根文件夹中有 OpenSans-Light.ttf:~/fonts/open-sans/OpenSans-Light.ttf

PrivateFontCollection privateFontCollection = new PrivateFontCollection();
privateFontCollection.AddFontFile("~/fonts/open-sans/OpenSans-Light.ttf");

但我得到 "System.IO.FileNotFoundException: File not found"

如何定位该字体文件?

我找到了使用 MapPath 的解决方案:

PrivateFontCollection privateFontCollection = new PrivateFontCollection();
privateFontCollection.AddFontFile(HttpContext.Current.Server.MapPath("~/fonts/open-sans/OpenSans-Light.ttf"));