0

how to read a file from a different project?

I have a solution:

Solution1
-MyProject
-MyProject.Artifacts
----Message.XML
-MyProject.Tests

I am attempting to read the contents of Message.XML from MyProject.Tests.

How do I read the contents of Message.XML from MyProject.Tests?

Unfortunately, right now I'm doing something like this, but it's not very pretty:

        var currentDir = Directory.GetCurrentDirectory();
        var parentDir = Directory.GetParent(Directory.GetParent(currentDir).FullName).FullName;
        var parentParentDir = Directory.GetParent(Directory.GetParent(Directory.GetParent(currentDir).FullName).FullName).FullName;
        var parentParentParentDir = Directory.GetParent(Directory.GetParent(Directory.GetParent(Directory.GetParent(currentDir).FullName).FullName).FullName).FullName;
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • What kind of application is it? Web, Console or WPF ...? I think you can define the XML output folder yourself. Thus, you can refer to this: https://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path – wannadream Nov 16 '18 at 20:12
  • its an azure function – Alex Gordon Nov 16 '18 at 20:46

2 Answers2

0

You can store a path to the file in app settings of app.config / web.config using that read the file contents.

That way if you need to deploy your software in a different way you have the flexibility

-1

If your path is fixed, you can write the path like "c:\projects\solution ... Message.xml"

If you want a relative path, the simplest way is this:

var DI = new DirectoryInfo("..\\..\\..\\..\\Your Folder\\Message.XML");

This path is started from CurrentDirectory and goes four folders up and the one folder down and finds the file.

MSL
  • 990
  • 1
  • 12
  • 28