'Load a file from another project
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.
Solution 1:[1]
string str = Path.GetDirectoryName(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName) + "\\->ProjectName<-";
I think this idea can help you.
Solution 2:[2]
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).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Adrian Mole |
| Solution 2 | Graffito |
