views:

342

answers:

3

My standalone smallish C# project requires a moderate number (ca 100) of (XML) files which are required to provide domain-specific values at runtime. They are not required to be visible to the users. However I shall need to add to them or update them occasionally which I am prepared to do manually (i.e. I don't envisage a specific tool, especially as they may be created outside the system).

I would wish them to be relocatable (i.e. to use relative filenames). What options should I consider for organizing them and what would be the calls required to open and read them?

The project is essentially standalone (not related to web services, databases, or other third-party applications). It is organised into a small number of namespaces and all the logic for the files can be confined to a single namespace.

========= I am sorry for being unclear. I will try again. In a Java application it is possible to include resource files which are read relative to the classpath, not to the final *.exe. I believe there is a way of doing a similar thing in C#.

========= I believe I should be using somthing related to RESX. See (RESX files and xml data http://stackoverflow.com/posts/1205872/edit). I can put strings in a resx files, but this is tedious and error-prone and I would prefer to copy them into the appropriate location.

I am sorry to be unclear, but I am not quite sure how to ask the question.

========= The question appears to be very close to (http://stackoverflow.com/questions/474055/c-equivalent-of-getclassloader-getresourceasstream). I would like to be able to add the files in VisualStudio - my question is where do I put them and how do I indicate they are resources?

+1  A: 

If you put them in a subfolder relative to your executable, say .\Config you would be able to access them with File.ReadAllText(@"Config\filename.xml").

If you have an ASP.NET application you could put them inside the special App_Data folder and access them with File.ReadAllText(Server.MapPath("~/App_Data/filename.xml"))

Darin Dimitrov
A: 

I'm still unclear whether you have a Win forms, or web project... Regardless, use of relative file names is not required to have them relocatable. I would suggest adding an entry in your web.config or app.config file's appSettings section containing the full path to your xml files. If they are moved, all you have to do is change your config file.

<!-- app/web.config --->
<appSettings>
        <add key="filelocation" value="c:\xmlfiles"/>
</appSettings>

//c# code (in ASP.NET app, for example)
using System.IO;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string path = ConfigurationManager.AppSettings["filelocation"];
        DirectoryInfo di = new DirectoryInfo(path);
        List<XDocument> xmlDocs = new List<XDocument>();
        foreach (FileInfo fi in di.GetFiles())
        {
            xmlDocs.Add(XDocument.Load(fi.FullName));
        }
    }
}

Of course you may or may not know the xml file names, so you may not load up and read them in quite the same way.

In your case it seems totally appropriate to put them in your App_Data folder if you have a ASP.NET site (assuming it's never going to be on more than one server). Just be careful if you list "~/App_Data" in your config file you know to translate that into a full path like darin did with Server.MapPath().

Kurt Schindler
A: 

There is a detailed answer on http://www.attilan.com/2006/08/accessing_embedded_resources_u.php. It appears that the file has to be specified as an EmbeddedResource. I have not yet got this to work but it is what I want.

peter.murray.rust