How to embed a file or text to c# project dll at build time in visual studio?

Priyadarshini Gopal 1 Reputation point

I need to embed a file/text to c# project dll at build time.
I have tried following ways to do it:
Consider I am having a c# project MyProject,

  1. I have created a Support.exe which actually creates a Resource.resx file and embeds the file to by updating MyProject.csproj. I called this support.exe in pre-build event of Myproject. But it did not embed the file to MyProject.dll. If I do a rebuild, the files were embeded.
  2. Another way, I have used Assembly Linker to embed a file to the dll. I called the command al /out:MyProject.dll /embed:filename in post-build event of MyProject. But the assembly linker creates a new dll and it does not included the MyProject.cs file.

Is there any other ways to embed a file to c# project dll at buildtime ?

Thanks in advance !

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
35,807 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sam of Simple Samples 5,516 Reputation points

    The following worked for me in a console program using VS 2017 but it should work in a DLL too.

    Go to the project in Solutinon Explorer and right-click and add a Resources File. You have probably done that much. Then open the Resources File. At the top of that designer we have Add Resource; when I click the drop-down then Add New Text File is an option. I added a text file and put something in it.

    Then during execution ConsoleApp1.Resource1.TextFile1 has the contents of the text file, where ConsoleApp1 is my project's namespace and Resource1 is the name of the resource file and TextFile1 is the name of the text file (without extension). You can use the Class View to find the name to be totally sure.

    And while editing the text file in VS you can right-click on the tab with the filename and select Copy Full Path to be sure where the file is at.

  2. Anna Xiu-MSFT 25,381 Reputation points Microsoft Vendor

    Hi @PriyadarshiniGopal-5678 ,

    Welcome to Microsoft Q&A!

    For your requirement, you can try to set the build action for a file to Embedded Resource.

    Besides, you can also refer to the similar issue:


    • If the answer is helpful, please click "Accept Answer" and upvote it.
      Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    0 comments No comments