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.
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"));
You should set the Copy to Output Directory
to "copy if newer" and you can then use:
Path.Combine(Application.StartupPath, "file.xml");
Path.Combine(typeof(MyClass).Assembly.Location.ToString(), "file.xml")
精彩评论