How to generate a file in the same exe directory

Asked

Viewed 1,234 times

4

I am generating an XML file and I want it to generate with exe.

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

instead of putting the directory to save with exe.

2 answers

4

Can use Environment.CurrentDirectory:

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

Extra:

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

  • Thank you both for the answer, but one way that solved was by the "." ex: ". .xml file" and it generates together with the executable, I am using Visual Studio, but I think it is a particularity of the language.

  • 3

    @Brunorodrigues tb is a solution, in this case using the point is to create a relative path. My solution and Renan’s would create absolute paths. Nothing wrong with that as long as what you’re using to create the file understands relative paths.

4

  • 3

    This is the only reliable solution to the question. Other ways can work by coincidence. The day the coincidence ends, no one knows why the show blew up.

  • @bigown however, if the file is a shadow copy, the returned path will be the copy path (MSDN)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.