开发者

C# File Location Connundrum

开发者 https://www.devze.com 2022-12-20 13:55 出处:网络
I have an xml file in a WCF application that describes dependencies. It is loaded when the service facade constructor runs and works great when testing the app alone. The way our separate web testing

I have an xml file in a WCF application that describes dependencies. It is loaded when the service facade constructor runs and works great when testing the app alone. The way our separate web testing application is setup is an endpoint in a different (mvc) project - So the relative paths are different to the xml file that we need to load. The question is what would be the best way to load that file from both projects (so that when you run the first project alone it loads the file, but then when that dll is开发者_如何学JAVA loaded in the second project it can still find the xml file)?


You could copy it to the output directory. You can do this from Visual Studio by right clicking the file, choosing properties and changing "Copy To Output Directory" to "Copy Always".

At runtime you will be able to find the file in the current directory in both projects.


Try Server.MapPath

example:

Server.MapPath (@"~\App_Data\menudata.xml")

then you can place your file in any location relative to the root directory of your website/webservice.


I tried the "Copy To Output" with no luck - for some reason my root path was not where the output path was (which i previously thought)... So to abstract away any headaches from other developers setting up this project I did this:

var _assembly = System.Reflection.Assembly.GetExecutingAssembly();
XDocument xdoc = XDocument.Load(new StreamReader(_assembly.GetManifestResourceStream("TheNamespace.Api.TheFile.xml")));

Which of course requires the file to be an embedded resource.

0

精彩评论

暂无评论...
验证码 换一张
取 消