Embedded resource in .Net Core libraries

前端 未结 5 608
醉梦人生
醉梦人生 2020-12-24 10:05

I just have started looking into .Net Core, and I don\'t see classical resources and anything what looks like resources. In classical .Net class libraries I was able to add,

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-24 10:54

    UPDATE:

    .NET Core 1.1 and later have dropped project.json and returned to .csproj files. This changes Step 2, but not all that much. The necessary lines are very similar:

    
      
      
      
    
    
      
      
      
    
    

    There may be a similar *.tff form; unconfirmed.

    Steps 1 and 3 are unchanged.


    To use embedded resources in .NET Core 1.0 project do the following:

    1. Add your embedded file(s) as usual.

      Example: some FONT files on a directory named "_fonts"

    2. Modify "project.json" to include the related resources.

      In my case:

       "buildOptions": {
          "embed": {
            "include": [
              "_fonts/*.ttf"    
            ]
          } 
        },
      
    3. Access the embedded resource in code.

      var assembly = typeof(MyLibrary.MyClass).GetTypeInfo().Assembly;
      Stream resource = assembly.GetManifestResourceStream("MyLibrary._fonts.OpenSans.ttf");
      

      The key point is to use the right name on GetManifestResourceStream call. You have to use [assembly name].[directory].[file name].

提交回复
热议问题