How to generate a file in the same exe directory

4

I am generating an XML file and I want it to be generated by the exe.

XmlTextWriter writer = new XmlTextWriter(@"c:\dados\filmes.xml", null);

instead of per directory to save to the exe.

    
asked by anonymous 10.10.2014 / 18:30

2 answers

4

With this:

string caminho = System.Reflection.Assembly.GetExecutingAssembly().Location;

From there you get the directory where the executable is. You can use the GetDirectoryName method of the Path to get the directory.

I stolen SOen.

    
10.10.2014 / 18:38
4

You can use Environment.CurrentDirectory :

string caminho = Path.Combine(Enviroment.CurrentDirectory, "filmes.xml");

Extra:

If you have to work with paths, you can use Path.Combine in order to abstract some of the complexity of creating paths correctly.

    
10.10.2014 / 18:35