0

We have set of Test projects under one solution in Visual Studio. I want to read a Json file which is copied to the output directory from a different project folder in runtime. It's a test project. I can get the current project directory. But not sure how to get the other assembly's directory.

Solution looks as below Project1 -> Sample.json (this file is set to copy to output directory) Project2

While running my test in Project2 I want to access the file in Project1 from the output directory.

I used to access files in the same project folder with code as mentioned. But not sure how to get for a different project file. Now with replace I am able to achieve it. But sure this is not the right way

 var filePath = Path.Combine("Sample.json");
 var fullPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), filePath).Replace("Project1", "Project2");

Not sure how to get from other project. I am sure I can't use GetExecutingAssembly(), but what is the alternative. Somehow I can access it using the above dirty way of replacing the assembly name.

1
  • 1
    Why not adding the json file as link to Project2 and set it's "Copy to Output Directory" property to "Copy Always" or "Copy If Newer"? this way you're having only one copy of the file under Project1, but it will still be copied to the output folder of Project2. Commented Nov 6, 2019 at 12:01

4 Answers 4

1

To get the location of another assembly, you get use a type from that assembly to get to the right Assembly instance, and thus its location:

typeof(FromOtherAssembly).Assembly.Location
Sign up to request clarification or add additional context in comments.

2 Comments

typeof().Assembly.Location..Can you please tell me from this example what I need to provide as an argument inside typeOf()?
@user2523635 The name of a type from the other assembly.
1

First, I suggest that you could find the dll path in the solution.

Second, you can filter the json file from the path.

The next is my working code.

Please install Microsoft.Build and Microsoft.Build.Framework nugetpackages first.

        string path= string.Empty;
        var solutionFile =SolutionFile.Parse(@"D:\test.sln");// Your solution place.
        var projectsInSolution = solutionFile.ProjectsInOrder;
        foreach (var project in projectsInSolution)
        {
           if(project.ProjectName=="TestDLL")     //your  dll name
            {
                path = project.AbsolutePath;
                DirectoryInfo di = new DirectoryInfo(string.Format(@"{0}..\..\", path));
                path = di.FullName;
                foreach (var item in Directory.GetFiles(path,"*.json")) // filter the josn file in the correct path
                {
                    if(item.StartsWith("Sample"))
                    {
                        Console.WriteLine(item);// you will get the correct json file path
                    }
                }
            }

        }

Comments

1

You can use the below code to do it in a better way

    //solutionpath will take you to the root directory which has the .sln file
      string solutionpath = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName);
      string secondprojectname = @"SecondProjectName\bin";
      string finalPath = Path.Combine(solutionpath, secondprojectname);

2 Comments

The output directory of the second project path extends as 'bin\Debug\netcoreapp2.2'. I wish to provide the project name, file name only as hard coded.
Extending the code you can write Directory.GetDirectories(finalPath); This will give you all directories inside bin and then you can decide based on your project build settings (eg. Debug or Release) and then you can append the rest. I am afraid that you will have to hardcode some parts to make it running for your code.
0

you can use CopyToOutputDirectory in MSBuild

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.