views:

966

answers:

3

When I run the following code, an XML file is correctly created in c:\temp:

XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<Models.Customer>));
using (StreamWriter wr = new StreamWriter("C:/temp/CustomerMock2.xml"))
{
    xs.Serialize(wr, CustomerList);
}

However, I actually want it to be created in a sub-directory underneath the project, but when I do this:

using (StreamWriter wr = new StreamWriter("Data/CustomerMock2.xml"))

it just acts as if it writes it but the file never appears in that directory:

C:\Projects\Prototype12\CustomersModul\bin\Debug\Data.

How can I create a file with StreamWriter with a relative path inside my project?

A: 

Does ./Data/CustomerMock2.xml work?

using (StreamWriter wr = new StreamWriter("./Data/CustomerMock2.xml"))
Aamir
A: 

Are you setting the XML data to be copied at compile time (so it's not in the actual project directory, but the bin folder)? In which case you can get to it using

string xmlFile = string.Format("{0}/Data/{1}",AppDomain.CurrentDomain.BaseDirectory,"myxml.xml");
Chris S
A: 

Relative paths are relative to the current directory. Maybe you're not in the bin/debug directory... You should build the absolute path based on the exe directory, as shown by Chris. Also, the StreamWriter constructor won't create the directory, you need to create it explicitly if it doesn't exist.

Thomas Levesque