Reputation: 9063
I am creating an Xml file and I want to save it in a specified folder inside my project within the solution where I can access it in the Solution Explorer.
How can I specify the path so that a folder is created and the xml file saved inside it?
As it is at the moment it creates the file in the root directory of my project and I cannot view it in Solution Explorer.
XmlSerializer serializer = new XmlSerializer(typeof(BoxSet));
TextWriter textWriter = new StreamWriter("../../Box.xml");
Sorry, still a newbie...am I missing something.?
Upvotes: 6
Views: 29509
Reputation: 937
Instead of using AppDomain.CurrentDomain.BaseDirectory
you can just do it this way:
TextWriter textWriter = new StreamWriter("foldername\\Box.xml");
When you don't add anything, the basedirectory is automatically assumed.
Upvotes: 1
Reputation: 2714
You can specify the path when you declare a new StreamWriter.
TextWriter textWriter = new StreamWriter("../../Box.xml");
This comes down to:
So when you want the file created in a folder inside the root folder you could use:
But if you don't want to rely on your current file location you can also use:
AppDomain.CurrentDomain.BaseDirectory
That would make:
var path = String.Format("{0}foldername\Box.xml", AppDomain.CurrentDomain.BaseDirectory);
TextWriter textWriter = new StreamWriter(path);
Hope this helps.
Upvotes: 16