Reputation: 2113
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.
Upvotes: 8
Views: 21118
Reputation: 21
string str = Path.GetDirectoryName(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName) + "\\->ProjectName<-";
I think this idea can help you.
Upvotes: 2
Reputation: 1398
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");
Upvotes: 13
Reputation: 1718
Temporarily update the application current directory:
string saveDir = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory("the_projectB_defaultDirectory");
ProjectBfunction() ;
Directory.SetCurrentDirectory(saveDir) ;
If your directory architecture permits it, you may deduce the projectB directory from ProjectA current directory or from the .exe directory (i.e. Application.StartupPath).
Upvotes: -1