I’m trying to load an XML document which is in Project\Data as shown below:
XmlDocument memoryDoc = new XmlDocument();
memoryDoc.Load(@"Data/Memories.xml");
XmlNode xmlNode_Memories = memoryDoc.SelectSingleNode("Memories");
XmlElement xmlElement_Memory = memoryDoc.CreateElement("Memory");
xmlElement_Memory.SetAttributeNode("Title", "");
xmlElement_Memory.SetAttribute("Title", "hijdnjh9d2qhei");
xmlNode_Memories.AppendChild(xmlElement_Memory);
memoryDoc.Save(@"Data/Memories.xml");
but the program is trying to load it from Project\bin\Debug\Data, which doesn’t exist, and it cries. How do I get it to load from Project\Data without having to include the full path, even if just for testing purposes?
Also would this cause problems when packaging my program in an installer, and therefore require me to copy the file to a different location before loading?
That is happening because your exe is running from the Project\bin\debug directory (that’s where your build output goes when you press F5/Run/Debug in Visual Studio, assuming you are doing a debug build.
If you want to avoid a hard coded path, you could use a post build event to copy the file to the Project\bin\debug\data directory.
Right-click on the project in Solution explorer and choose properties. Go to the Build Events tab. It’s kind of like writing a batch file, you can copy the file from there. (click “edit”, and then press the Macros button — there are expansion variables that provide your paths)
The command would look something like (from memory, so might be a little off):