6

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.

3 Answers 3

6

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"));
Sign up to request clarification or add additional context in comments.

1 Comment

In my case, this approach does not work because the Visual Studio Project containing the file is a SQL Database Project.
2

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

Path.Combine(Application.StartupPath, "file.xml");

3 Comments

it's not working..it is giving exception "file not found" and showing some "C:programfiles....." path.
Is it a windows app or ASP.NET app?
Try using 'Server.MapPath("~/file.xml");' The path will need to be relative.
0
Path.Combine(typeof(MyClass).Assembly.Location.ToString(), "file.xml")

1 Comment

Doesn't work, because Location.ToString() includes MyClass.exe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.