Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 3 projects under my solution. One of them is the main base project where I want to have the json file "Testdata.json".

I have to use this file across all 3 projects.. How can I get the path dynamically without hard coding it, and avoid putting it all 3 projects.

//Update:
As OriginalGriff mentioned I have created a public static method to write to the json file and read it and return it, in my main project "Project1" which is Referenced by the other two project.

//This is in Project1
public class JsonDataProvider
    {
        public static string filePath;

        public static string writeToJson(IRestResponse response)
        {
            string json = response.Content;
            string path = System.Reflection.Assembly.GetCallingAssembly().CodeBase;                                                                                                                                               
            string actualPath = path.Substring(0, path.LastIndexOf("bin"));
             string projectPath = new Uri(actualPath).LocalPath;
              filePath = projectPath + @"TestData.json";
            File.WriteAllText(filePath, json);

             string data = GetData();

            return data;
            

        }

        public static string GetData()
        {
            string data = File.ReadAllText(filePath);

            return data;
        }

      
    }



//In my "Project2" I am using this..
[Test]
public void GetAPITest()
{
      restResponse =  HttpGet();
 string expectedJson =JsonDataProvider.writeToJson(restResponse);
}


What I have tried:

I have tried .GetCallingAssembly but this way I have to the json file in every project.

string path = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
string actualPath = path.Substring(0, path.LastIndexOf("bin"));
string projectPath = new Uri(actualPath).LocalPath;
string fileToRead = projectPath + @"TestData.json";


Also tried
string path = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName, "TestData.json");


but I get path = C:\Users\TestData.json , Exception: System.UnauthorizedAccessException : Access to the path 'C:\Users\TestData.json' is denied.
Posted
Updated 18-Jul-20 16:51pm
v3

1 solution

Provide a public static method in one of the assemblies which retrieves the JSON path (or even the JSON data itself as a string) and returns it. Provided the project is referenced by the other two, they can call it.
 
Share this answer
 
Comments
Member 14779968 18-Jul-20 22:45pm    
Updated the question.. I created a public static method writes response to the json file and also reads it and returns as a string. This is in my main base project "Project1" The "Project1" is where i am keeping my TestData.json file also. But when I use this method in "Project2" and I open the TestData.json file which is in "Project1" i have no data in it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900