Lumine.FileIncluder 1.0.0

dotnet add package Lumine.FileIncluder --version 1.0.0
                    
NuGet\Install-Package Lumine.FileIncluder -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Lumine.FileIncluder" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lumine.FileIncluder" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Lumine.FileIncluder" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Lumine.FileIncluder --version 1.0.0
                    
#r "nuget: Lumine.FileIncluder, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Lumine.FileIncluder@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Lumine.FileIncluder&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Lumine.FileIncluder&version=1.0.0
                    
Install as a Cake Tool

FileImport Source Generator

License: GPL v3

A zero-dependency C# Source Generator that reads external files at compile time and embeds their contents directly into your code as strings, byte arrays, spans, or streams.

Say goodbye to runtime File.ReadAllText(), missing assets in output directories, and slow Assembly.GetManifestResourceStream().

Features

  • Zero Runtime Overhead: Files are converted into hardcoded C# literals or static byte arrays during compilation.
  • Multiple Formats: Support for string, byte[], ReadOnlySpan<byte>, ReadOnlyMemory<byte>, string[] (lines), Base64, and MemoryStream.
  • IntelliSense Ready: The generated code is immediately available in your IDE.
  • Whitespace Trimming: Optional automatic whitespace compression for text files.

Installation

Install the package via NuGet:

dotnet add package Lumine.FileIncluder

Configuration (Important!)

Due to Roslyn Source Generator security restrictions, the generator cannot read arbitrary files from your disk. You must explicitly tell the compiler which files are allowed to be read by adding them as <AdditionalFiles> in your .csproj file.

Open your .csproj and add:

<ItemGroup>
  
  <AdditionalFiles Include="Scripts/app.lua" />
  <AdditionalFiles Include="Data/config.json" />
  <AdditionalFiles Include="Data/image.png" />
</ItemGroup>

Usage

Create a partial class and define partial properties (or fields) with the [FileImport] attribute. The source generator will automatically implement them.

using System;
using System.IO;
using Lumine.FileIncluder.Attributes;

namespace MyApp;

public static partial class EmbeddedFiles {
    // 1. Import as a standard UTF-8 string
    [FileImport("Scripts/app.lua")]
    public static partial string LuaScript { get; }

    // 2. Import as a byte array
    [FileImport("Data/image.png", ImportMode.ByteArray)]
    public static partial byte[] ImageData { get; }

    // 3. Import as highly optimized ReadOnlySpan (compiles to static data segment)
    [FileImport("Data/image.png", ImportMode.ReadOnlySpan)]
    public static partial ReadOnlySpan<byte> ImageSpan { get; }

    // 4. Import as Base64 string
    [FileImport("Data/image.png", ImportMode.Base64String)]
    public static partial string ImageBase64 { get; }

    // 5. Import text and trim whitespace/newlines
    [FileImport("Data/config.json", TrimWhitespace = true)]
    public static partial string ConfigJson { get; }
    
    // 6. Import as an array of lines
    [FileImport("Scripts/app.lua", ImportMode.Lines)]
    public static partial string[] LuaScriptLines { get; }
    
    // 7. Get a ready-to-use MemoryStream
    [FileImport("Data/image.png", ImportMode.Stream)]
    public static partial MemoryStream ImageStream { get; }
}

How it works behind the scenes

For the LuaScript property above, the generator will create a hidden file (EmbeddedFiles.LuaScript.g.cs) during compilation that looks like this:

// <auto-generated/>
#nullable enable
using System;

namespace MyApp {
    public static partial class EmbeddedFiles {
        /// <summary>Imported from: Scripts/app.lua (45 chars)</summary>
        public static partial string LuaScript => "print(\"Hello World!\")\nreturn true;";
    }
}

Available Import Modes

Mode Generated C# Type Description
String (Default) string UTF-8 text, safely escaped into a C# string literal.
ByteArray byte[] A standard byte array (new byte[] { 0x... }).
ReadOnlySpan ReadOnlySpan<byte> Optimized by the C# compiler to use the assembly's static data segment. Best for performance and low GC pressure.
ReadOnlyMemory ReadOnlyMemory<byte> A read-only memory wrapper over a cached byte array.
Base64String string Binary data converted to a Base64 string at compile time.
Lines string[] Text file split by newlines into a string array.
Stream MemoryStream Returns a new read-only MemoryStream wrapping the binary data on each access.

Troubleshooting

  • Error: File not found in AdditionalFiles (FIMP001) Make sure the path in the [FileImport("path/to/file")] matches the path inside the <AdditionalFiles Include="..."> tag in your .csproj.
  • Syntax Error: partial modifier is missing Ensure that both your containing class/struct AND the property/field are marked with the partial keyword.

License

This project is licensed under the GNU General Public License v3.0 (GPLv3). See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 183 4/27/2026