首页 > 解决方案 > C# PrivateFontCollection AddFontFile 从相对路径

问题描述

我从根目录的这个文件夹中有 OpenSans-Light.ttf:~/fonts/open-sans/OpenSans-Light.ttf

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

但我得到“System.IO.FileNotFoundException:找不到文件”

如何定位该字体文件?

标签: c#model-view-controllerfontsprivatefontcollection

解决方案


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

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

推荐阅读