Friday, March 22, 2013

Using Embedded Resources - C#

If you have a windows form application in which you want to use data which is embedded in the application but not open for the users to edit, you can use the following.

In VS IDE, Solution Explorer, select the project name, click on it to show the Menu. Select New --> Text File
Name the text file. Now it is added under the project resources.

Set the Resource Type as Embedded Resource.
Also set the copy preference to copy to output folder.

Include this code in your program to access the file.
You can use streamreader to read or write to the file.

Assembly _assembly;
StreamReader reader;
_assembly = Assembly.GetExecutingAssembly();
reader = new StreamReader(_assembly.GetManifestResourceStream("WindowsFormsApplication1.textfile.txt"));

No comments:

Post a Comment