8

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.

peter
  • 2,103
  • 7
  • 25
  • 51
  • 2
    in project B, select `Mock\myDoc.html` => Copy to Output Directory http://stackoverflow.com/questions/4596508/vs2010-how-to-include-files-in-project-to-copy-them-to-build-output-directory-a – Khanh TO Aug 05 '15 at 13:40
  • 1
    It is set to CopyToOutput directory, already. Project B has a different output directory than Project A. – peter Aug 05 '15 at 13:41
  • 1
    All file operations will be relative to the process's current (working) directory. This has no relation to the folders you have your project's in. To use relative paths you need to set the current directory. That it is currently the output location of a project is just defaults from the launching process. – Richard Aug 05 '15 at 13:46
  • That definitly makes sense Richard. If I understand you correctly I would have to hard-code the path? I'd like a solution that will work for my colleague, too (where the root folder name, e.g., might be different). I guess I could manipulate the path a bit to get it work. – peter Aug 05 '15 at 13:49

3 Answers3

12

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");
weirdev
  • 1,388
  • 8
  • 20
  • 1
    This doesn't work for me. My current directory is in `Temp` which represents the solution directory and the other project is not in there. – Mayron Aug 16 '19 at 08:37
1
string str = Path.GetDirectoryName(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName) + "\\->ProjectName<-";

I think this idea can help you.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
VuXuanCu
  • 11
  • 1
-1

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).

Graffito
  • 1,658
  • 1
  • 11
  • 10