Add a file to project resources using C#

14,213

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.

Share:
14,213
Ryan Cox
Author by

Ryan Cox

I started working in IT in 2012 as a manual QA Tester, and have been studying the .Net framework languages and general development/testing techniques since. I currently work scripting automated tests using protractor on an angular application, as well as executing manual test cases and building quality solutions for business. At home, I'm teaching myself web development and a little design, but I've a long way to go.

Updated on June 17, 2022

Comments

  • Ryan Cox
    Ryan Cox almost 2 years

    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!

  • Ryan Cox
    Ryan Cox over 10 years
    Thanks, but I need to specifically add it using C# in my test class because it doesn't exist in any state I want to save until after the test has run. I'll try to add an example of what I'm trying to do.
  • Ryan Cox
    Ryan Cox over 10 years
    I found [this forum post]( stackoverflow.com/questions/6926378/…) that seems to do what I am looking to do. However upon further research, it would probably be counter intuitive to the result I want/need. Since saving a file into my resources would change the state of the test each time it's run, it essentially invalidates the test. To keep a sterile test environment, I'll delete any temporary files that I unpack, and manually embed my files for the tests that require them as suggested above. Thanks!