Load XML from Disk in C#
I am using C# test project .I wish to load a Xml which is available inside the project under a folder Dump . I 开发者_StackOverflow社区am able to do
string path = "C:\APP\FrameworkTest\TestProject\Dump\GetAddressById.xml";
but i don't want to use like this because if the drive changes,my code will fail.
in asp.net we have something like Server.MapPath() . Is there some thing like this ?
For example:
var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var path = Path.Combine(dir, "Dump", "GetAddressById.xml")
Hope this helps.
If you know that folder Dump
will always be present in the deployement folder of your application then you certainly don't need to hard code full path.
For ASP.net:
var path = System.IO.Path.Combine(Server.MapPath("/"), "Dump",
"GetAddressById.xml");
For C#:
var path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Dump",
"GetAddressById.xml");
精彩评论