Lumine.FileIncluder
1.0.0
dotnet add package Lumine.FileIncluder --version 1.0.0
NuGet\Install-Package Lumine.FileIncluder -Version 1.0.0
<PackageReference Include="Lumine.FileIncluder" Version="1.0.0" />
<PackageVersion Include="Lumine.FileIncluder" Version="1.0.0" />
<PackageReference Include="Lumine.FileIncluder" />
paket add Lumine.FileIncluder --version 1.0.0
#r "nuget: Lumine.FileIncluder, 1.0.0"
#:package Lumine.FileIncluder@1.0.0
#addin nuget:?package=Lumine.FileIncluder&version=1.0.0
#tool nuget:?package=Lumine.FileIncluder&version=1.0.0
FileImport Source Generator
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, andMemoryStream. - 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:
partialmodifier is missing Ensure that both your containing class/struct AND the property/field are marked with thepartialkeyword.
License
This project is licensed under the GNU General Public License v3.0 (GPLv3). See the LICENSE file for details.
| Product | Versions 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. |
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 |