Search code examples
c#.netembedded-resource

Add a file to project resources using C#


I'm creating SpecFlow tests for an application that uses an xml settings file (example, C:\ssis\mySettingsFile.xml) to run. In one test, I want to save the file to disk, and then add that file to my project resources and clean up the disk location. Then, another test will unpack the resource to a temporary directory and use it from there.

I'm clear about the unpacking part, but is there a way to programatically pack the file into a project resource rather than manually adding it to the project using the VS GUI and marking it as an embedded resource?

I know this is wrong, but I'm thinking something along the lines of:

string myPath = "C:\ssis\mySettingsFile.xml";
TestHelper.ResourceDirectory = "$\...\...\Project.Folder.Resources";
myResource = TestHelper.PackResource(myPath);
myResource.IsEmbeddedResource = true;

...where PackResource method saves the file to the project resources.

Thanks in advance!


Solution

  • you can create a Resource Folder and add your xml to it. When you click on Properties of the project, there will be a Resources tab wherein you can see your file. To access the file, you can use ProjectNamespace.Properties.Resources.yourfilename.