Relative path using C #

I am using C #. In my project, I have an xml folder in which I have an xml file that says "file.xml" .. I want to use this file in my project. I want to take this file from the current project, because I pass it as:

  xmlDoc.Load(@"..\xml\file.xml");

but he does not take the file. It shows some path "C:". how can i extract this file from the project itself.

+6
source share
3 answers

You must set the property Copy to Output Directoryin the file in Solution Explorer to transfer the file to the folder with your EXE.

Then you can write

xmlDoc.Load(Path.Combine(typeof(MyClass).Assembly, "file.xml"));

EXE .

EDIT. ASP.Net App_Data ( ),

xmlDoc.Load(Server.MapPath("~/App_Data/file.xml"));
+6

Copy to Output Directory "copy if newer", :

Path.Combine(Application.StartupPath, "file.xml");
+2
Path.Combine(typeof(MyClass).Assembly.Location.ToString(), "file.xml")
0
source

All Articles