Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

relative path using c#

I am using c#. In my project I am having a xml folder in which i have an xml file say "file.xml".. I want to use the file in my project. I want to take that file from the current project itself,for that I am giving the path as:

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

but it is not taking the file. It is showing some "C:" path.. how can I retrive this file from project itself.

like image 318
mucchar Avatar asked May 12 '11 11:05

mucchar


2 Answers

You should set the Copy to Output Directory property on the file in the Solution Explorer to gocpy the file to the folder with your EXE.

You can then write

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

This uses the actual location of the EXE file and will work regardless of the current directory.

EDIT: In ASP.Net, you should put your file in the App_Data folder (which is not publicly accessible), then write

xmlDoc.Load(Server.MapPath("~/App_Data/file.xml"));
like image 193
SLaks Avatar answered Oct 20 '22 21:10

SLaks


You should set the Copy to Output Directory to "copy if newer" and you can then use:

Path.Combine(Application.StartupPath, "file.xml");
like image 32
jaywayco Avatar answered Oct 20 '22 21:10

jaywayco