Use xml file as a resource for visual studio solution

11,772

In general, try to avoid adding additional files to your install set. If a data file will be read only forever, make it a resource and link it into the executable file so there's no chance of it getting lost.

If your intent is that the end user should be able to edit the contents of this data file, then it should not be a resource linked into the executable.

In VS2010, right click on your project and select Add.., then select New Item, and select Resource File. Double click on the Resource1.resx that is created to bring up the resource editor. From the menu at the top of the resource editor, select Add Resource, then Add Existing File, and select your existing xml file on disk. This will copy the XML file into a Resources subdirectory of your project and link the file into your executable file at compile time. You can access this data as a string via the generated Resource1 class.

Share:
11,772
Tono Nam
Author by

Tono Nam

Updated on June 05, 2022

Comments

  • Tono Nam
    Tono Nam almost 2 years

    I am confused when using resource files. In my application I need to save some common file extensions. Since they are a lot, I created a xml file that looks something like:

    <?xml version="1.0" encoding="utf-8" ?>
    
    <Files>
    <File>
        <Signature>3026B2758E66CF11A6D900AA0062CE6C</Signature>
        <Extension>WMA|WMV|ASF</Extension>
        <Description>WindowsMediaFile</Description>
      </File>            
      <File>
        <Signature>0000000020000000FFFF0000FFFF</Signature>
        <Extension>RES</Extension>
        <Description>ResourceFile</Description>
      </File>
      <File>
    <Signature>64383A616E6E6F756E6365</Signature>
    <Extension>TORRENT</Extension>
    <Description>BitTorrentMetainfoFile</Description>
      </File>
    ..... etc
    ..... etc
    ......
    

    I could use a .setting file instead but I guess that will not be that efficient.

    Anyways I know how to retrieve data from that file. the problems comes when I plan on using this application on a different computer.

    I can read data from that xml file by specifying the path: A:\Users\Tono\Desktop......FileSignatures.xml

    but what path will I have to use so that it works when I deploy this application. I know I can place that file in the bin directory and then I can use the relative path. also when I select the file in visual studio I have an option:

    enter image description here

    If I need this file should I copy that file to the output directory so that I will be able to use it when deploying it to a different computer?

    I know I will be able to probably solve this problem but I just want to make sure I am doing it the right way. I might be able to solve it but I don't understand how it works.