Support Resource loading in a library, where you have no control over the calling executable. I do not wish to load files from disk.
How can I fix the ConsoleApp2 code so it supports the described scenario?
Based on the Pack Uri documentation, I was able to do this:
Build Action: Resource
Add reference to WindowsBase
runtime assembly.
class Program
{
static void Main(string[] args)
{
// Workaround to register pack://application Uri format
var notUsedButNeeded = System.IO.Packaging.PackUriHelper.UriSchemePack;
var fontFamilies = System.Windows.Media.Fonts.GetFontFamilies(new Uri("pack://application:,,,/"), "/FontLibrary;Component/");
// This correctly lists the font families contained in the font library
Console.WriteLine("Number of font families: " + fontFamilies.Count);
}
}
Exception message is
The system cannot find the file specified. (Exception from HRESULT: 0x80070002)'
Build Action: Resource
class Program
{
static void Main(string[] args)
{
new FontLoader().Load();
}
}
public class FontLoader
{
public void Load()
{
// Workaround to register pack://application Uri format
var notUsedButNeeded = System.IO.Packaging.PackUriHelper.UriSchemePack;
// Raises exception when called from ConsoleApp2, since the APPLICATION = the ConsoleApp2 does not reference the font library.
var fontFamilies = System.Windows.Media.Fonts.GetFontFamilies(new Uri("pack://application:,,,/"), "/FontLibrary;Component/");
Console.WriteLine("Number of font families: " + fontFamilies.Count);
}
}
User contributions licensed under CC BY-SA 3.0