views:

15

answers:

1

I want to use an xml file in a windows forms application. I added a new xml file to my project. When I reference the file like this xmlDataDoc.DataSet.ReadXml("~\TestLog.xml"); how do i just tell it to look at the xml file that is part of the project? The line I put above doesn't work.

+1  A: 

This will very much depend on the type of application. In ASP.NET application assuming the file is deployed on the site root you could use this:

string filePath = Server.MapPath("~/testlog.xml");

In a console application, assuming you copy the file to the output folder (Copy to Output Directory: Copy Always in the properties of the file) at the same place as the executable you could use a relative path:

string filePath = "testlog.xml";

To get the location of the currently executing assembly you could use this:

string location = Assembly.GetExecutingAssembly().Location;

and then construct a path relative to this location:

string filePath = Path.Combine(location, "testlog.xml");

So many ways of achieving the same thing, which one to choose depends on your context.

Darin Dimitrov