Tuesday, May 03, 2011

Adding Files Etc. as "Resources" to Visual Studio Projects

I recently wrote a unit test that needed some data that was too large to fit into a string literal.  To fix the problem, I added the file containing the string data as a resource to my project:

  • In Solution Explorer, right click on the project Properties;
  • Click on Open;
  • Click on the Resources tab;
  • Click on Add Resource and follow the dialogue for adding a file.
In the source code I can refer to the string contents of my resource file simply as

  • MyNameSpace.Properties.Resources.MyResourceName
Of course, this scheme also works for data needed in other situations (e.g., logo images), not just unit tests.