question

77966360 avatar image
0 Votes"
77966360 asked AnnaXiu-MSFT answered

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

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 !

vs-msbuild
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SimpleSamples avatar image
0 Votes"
SimpleSamples answered 77966360 commented

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.




· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thanks for your response !
I can able to embed a file manually to a project . But I want to embed the resource to a dll at the build time.

0 Votes 0 ·
AnnaXiu-MSFT avatar image
0 Votes"
AnnaXiu-MSFT answered

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:
https://stackoverflow.com/questions/1890688/can-i-embed-other-files-in-a-dll

Sincerely,
Anna


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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.