Click here to Skip to main content
15,351,370 members
Articles / Programming Languages / C#
Posted 15 Mar 2017

Tagged as


10 bookmarked

Converting Files to C# Source Code

Rate me:
Please Sign up or sign in to vote.
4.19/5 (9 votes)
15 Mar 2017Public Domain4 min read
A trick you can do with enhanced C#


One way you can include a file in a C# project is to embed it in a resx file. However, sometimes, you might prefer to embed the file in the source code itself, as either a string or a byte array.


To do this, download the LeMP preprocessor. From here, you can either install it in Visual Studio or run it as a command-line tool.

The quickest approach is to run LeMP.exe --editor, which shows a demo window where you can play with LeMP. In that case, all you have to do is insert code like...

byte[] file = includeFileBinary(@"C:\Path\To\BinaryFile.bin");
string code = includeFileText(@"C:\Path\To\Readme.txt"); 

...on the left side of the demo window. The output appears on the right.

includeFileBinary creates a new byte[] array, while includeFileText creates a string.

Adding LeMP to a C# Project

Image 1

  1. You can also install LeMP into Visual Studio so that it runs automatically whenever you save an ecs (Enhanced C#) file.

    Note: Visual Studio 2017 no longer supports the LeMP installer because it no longer uses the system registry. However, you can run LeMP as a pre-build event instead; see the end of step 5. Arguably, a pre-build event is actually better for the task of converting files to source code because the source code will be updated whenever you build your project. In contrast, if you use LeMP as a single-file generator, the output code is only updated when you save the ecs file in Visual Studio; it won't automatically notice when the included file changes.

  2. Create a C# file, let's call it Files.cs, that specifies the file(s) you want to include:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    namespace LeMPIncludeFileExample
        class Files
            public static readonly byte[] SmileGif = includeFileBinary(@"icon_smile.gif");
            public static readonly byte[] SadGif   = includeFileBinary(@"icon_sad.gif");
            public static readonly string Program  = includeFileText(@"Program.cs");

    The files need to exist in the same folder as the source code (or you can use a relative path).

  3. Rename the file to Files.ecs (Enhanced C#) to indicate it is not a standard C# file.
  4. Right-click Files.ecs in Solution Explorer and choose "Properties" to make sure the properties panel is visible.
  5. One of the properties is "Custom Tool". Set this property to "LeMP" (without the quotes).

    If LeMP is installed, then a new source file will appear in your project, Files.out.cs.

    Check the Error panel to see if any errors occurred. If not, you should see contents of your files in Files.out.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    namespace LeMPIncludeFileExample
        class Files
            static readonly byte[] smileGif = new byte[] {
            static readonly byte[] sadGif = new byte[] {
            static readonly string program = @"using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace LeMPIncludeFileExample
        class Program
            static void Main(string[] args)

Visual Studio will rebuild the file every time.

Note: You can also run LeMP on the command line or as a pre-build event. To take the latter approach, go to the project properties and, in the "Build Events" section, add a command like this to the "Pre-build event command line":

C:\LOCATION-OF-LEMP\LeMP.exe $(ProjectDir)Files.ecs --outext:out.cs

This should produce a Files.out.cs file that you can include in your project manually (the Show all files icon in Solution Explorer will allow you to see the file. Right-click and "Include in project".)

If you use this method, then "Custom Tool" should not be set to LeMP in the Properties window. It should be blank.

How Does It Work?

LeMP is a tool that lets you generate code by calling special methods called "macros" in your C# code. For example, the includeFileText method that was used in the example above is bundled with LeMP. Here is its source code:

    "Reads a UTF-8 text file into a string literal.
    Newlines become '\n'."
   +"The single argument is macro-preprocessed.")]
public static LNode includeFileText(LNode node, IMacroContext context)
    string filename;
    if (node.ArgCount == 1 &&
    (filename = context.PreProcess(node[0]).Value as string) != null) {
        var inputFolder = context.ScopedProperties.TryGetValue
                          ((Symbol)"#inputFolder", "").ToString();
        var path = System.IO.Path.Combine(inputFolder, filename);
        var text = File.ReadAllText(path, Encoding.UTF8);
        return F.Literal(text).SetBaseStyle(NodeStyle.TDQStringLiteral);
    return null;

// elsewhere
static LNodeFactory F =
new LNodeFactory(new EmptySourceFile("StandardMacros.cs"))

The key part of this method is at the end, where it calls the standard File.ReadAllText method to load a file, then calls the factory method F.Literal(text) to convert it into a source code literal (for example, F.Literal("Hello") essentially converts "Hello" into a syntax tree, which will later be printed as C# code.)

To find out what else you can do with LeMP, check out the home page. If you'd like to write a macro of your own, please leave a message here and I will be delighted to help you! Basically, the steps are:

  1. Create a class library called MyMacros.dll to hold your macro
  2. Add the Loyc.Syntax NuGet package to it
  3. Write a public static method like the one above with a [LexicalMacro("","")] attribute on it (the two strings are documentary and have no effect)
  4. Put a [ContainsMacros] attribute on the containing class, which must be public
  5. Use LNodeFactory to generate some code
  6. Use the command-line argument --macros:MyMacros.dll to load the macros into LeMP
  7. Put #importMacros(MyMacros); at the top of the input file (where MyMacros is the namespace that contains your macros) to gain access to your macro (it's like a using directive for macros), and
  8. Call your macro! If you'd like to do something complicated, you might want to read this article in addition to contacting me.


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Software Developer None
Canada Canada
Since I started programming when I was 11, I wrote the SNES emulator "SNEqr", the FastNav mapping component, the Enhanced C# programming language (in progress), the parser generator LLLPG, and LES, a syntax to help you start building programming languages, DSLs or build systems.

My overall focus is on the Language of your choice (Loyc) initiative, which is about investigating ways to improve interoperability between programming languages and putting more power in the hands of developers. I'm also seeking employment.

Comments and Discussions

Questionthats why Pin
BManfred17-Mar-17 1:43
MemberBManfred17-Mar-17 1:43 
Questionwhy ? Pin
BillWoodruff16-Mar-17 21:53
mveBillWoodruff16-Mar-17 21:53 
AnswerRe: why ? Pin
Pawel Sienko16-Mar-17 23:53
MemberPawel Sienko16-Mar-17 23:53 
GeneralRe: why ? Pin
Member 1297581217-Mar-17 5:00
MemberMember 1297581217-Mar-17 5:00 
GeneralRe: why ? Pin
Pawel Sienko20-Mar-17 0:43
MemberPawel Sienko20-Mar-17 0:43 
QuestionEmbedded resources Pin
Frantisek Ruzicka16-Mar-17 11:25
professionalFrantisek Ruzicka16-Mar-17 11:25 
AnswerRe: Embedded resources Pin
sx200824-Mar-17 12:42
Membersx200824-Mar-17 12:42 
GeneralRe: Embedded resources Pin
Frantisek Ruzicka25-Mar-17 14:36
professionalFrantisek Ruzicka25-Mar-17 14:36 
QuestionWhy Pin
johannesnestler16-Mar-17 4:12
Memberjohannesnestler16-Mar-17 4:12 
AnswerRe: Why Pin
Qwertie17-Mar-17 17:26
MemberQwertie17-Mar-17 17:26 
QuestionRe: Why Pin
sx200824-Mar-17 13:28
Membersx200824-Mar-17 13:28 
AnswerRe: Why Pin
Qwertie25-Mar-17 15:03
MemberQwertie25-Mar-17 15:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.