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.