Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a file from another project

Tags:

c#

I have two projects in my solution and project A has a reference to project B. Project A calls a function in project B that should load a document that is within Project B:

return XDocument.Load(@"Mock\myDoc.html");

The problem is that XDocument.Load is using the path from Project A (where there is no Mock\myDoc.html).

I have tried using

string curDir = Directory.GetCurrentDirectory();
var path = String.Format("file:///{0}/Mock/myDoc.html", curDir);

but this as well gives me a path to ProjectA\Mock\myDoc.html when instead it should be ProjectB\Mock\myDoc.html. What am I missing?

EDIT: "Copy to Output" for the file "myDoc.html" is set to "Copy always" and the file is available in the Output folder of Project B.

like image 719
peter Avatar asked Aug 05 '15 13:08

peter


Video Answer


1 Answers

There is only one current working Directory for all of your code at runtime. You'll have to navigate up to the solution directory, then back down to the other Project.

string solutiondir = Directory.GetParent(
    Directory.GetCurrentDirectory()).Parent.FullName;
// may need to go one directory higher for solution directory
return XDocument.Load(solutiondir + "\\" + ProjectBName + "\\Mock\\myDoc.html");
like image 104
weirdev Avatar answered Sep 21 '22 20:09

weirdev