C # File Location Conundrum

I have an XML file in a WCF application that describes dependencies. It loads when the service facade designer works and works great when testing the application. The way to install our standalone web testing application is the endpoint in another project (mvc). Thus, relative paths are different from the XML file we need to load. The question is, what would be the best way to load this file from both projects (so when it first starts the first project, it loads the file, but then when this DLL is loaded into the second project, it can still find the xml file)

+3
source share
3 answers

I tried Copy to Output without any luck - for some reason my root path was not where the exit path was (which I used to think) ... Therefore, to abstract any headaches from other developers creating 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
source

You can copy it to the output directory. You can do this from Visual Studio by right-clicking the file, selecting properties and changing "Copy to output directory" to "Always copy."

At run time, you can find the file in the current directory in both projects.

+1
source

Server.MapPath

:

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

-/-.

0

Source: https://habr.com/ru/post/1733586/


All Articles