Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed Text Files in a .NET Assembly or Executable [duplicate]

Possible Duplicate:
How to embed a text file in a .NET assembly?

I have a c# winforms application that use several text files.

I would like to somehow embed those files either into the exe or dlls, so that they cannot be seen by the user when the application is deployed.

Can visual studio do something like that?

like image 357
sd_dracula Avatar asked Jan 17 '13 03:01

sd_dracula


2 Answers

With text files you can also proceed as following:

  1. Double click your Properties -> Resources.resx file to open the resource designer.
  2. From the first small drop down on the top, select "Files" as resource type.
  3. Click on Add Resource -> Add Existing File... and select your text files.

Let's say that the TXT you added was called Foo.txt... you can just access it's content like so:

String foo = Properties.Resources.Foo;

Then you can also change te file names inside the resource designer and they will be automatically refactored in your code. From the Resources directory, where they are now located in your project, you can also edit them. I just recommend you to keep their access level as internal.

like image 196
Tommaso Belluzzo Avatar answered Nov 25 '22 13:11

Tommaso Belluzzo


Here's how I do it:

  1. In the solution Explorer, right-click on Properies/Resources.resx.
  2. Choose "View Designer".
  3. You can use the "Add Resource" button, but I often just drag the file right onto the designer window.
  4. Visual Studio will perform code generation, such that you can access your file using Properties.Resources.YourFileName.

Be wary: Visual Studio will copy your chosen file into your project's Resources folder. It will not create a link to wherever the file was originally stored.

like image 36
Charlie Salts Avatar answered Nov 25 '22 15:11

Charlie Salts