starmandeluxe
starmandeluxe

Reputation: 2538

Read a linked xml file in Visual Studio 2010 C# project

This may be a naive question, but I couldn't find any answers to it after some searching.

If I add a a link to an external config file in my Visual Studio 2010 project, I want to be able to read it in using one of the following methods:

XmlDocument doc = new XmlDocument();
doc.Load("c:\\config.config");

or

var doc = XDocument.Load(@"C:\config.config");

Now the question is, since I linked to my file in the project, is there a way to load it without providing the full path in the Load argument? I tried simply the file name, but it wasn't able to resolve that path. I would think that there is a good way to make use of a linked file imported into a project so I don't have to specify the full file location, but I could be mistaken and it is necessary to always include the full path to the file.

Upvotes: 0

Views: 2134

Answers (1)

Justin Ryder
Justin Ryder

Reputation: 777

In the Solution Explorer, right click on your xml file and click Properties. Make sure that Build Action is set as Content and Copy to Output Directory is one of the Copy options. Then when you want to load it, you should be able to do something like this:

XDocument.Load(Path.GetFullPath("config.config"))

See the MSDN for more info on the Path class. http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath(v=vs.110).aspx

Upvotes: 3

Related Questions