Saving a file in a specified folder inside my project

You can specify the path when you declare a new StreamWriter.

TextWriter textWriter = new StreamWriter("../../Box.xml");

This comes down to:

  • ../ - Go up one directory
  • ../ - Go up one directory
  • Box.xml file here

So when you want the file created in a folder inside the root folder you could use:

  • "../foldername/Box.xml"

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.

Tags:

C#

File Io