Repository class read from json file, how to read the file?

时间:2016-08-31 17:32:57

标签: c# json

In the DAL, I have a repository class that is getting the data from a json file. That json file is included in the sln.

The code is using the file's absolute location to create a stream reader, read the file and use JsonConvert.

var json = new StreamReaderWrapper(absoluteFileNamePath).ReadToEnd();
JsonConvert.DeserializeObject<MyInfoCollection>(json);

I would like to remove this absolute path, I tried to use the resource in the project but I always have a null stream.

var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("file");

As my json file is in the sln, I changed file the properties to copy always, but it's only copied in the bin folder of the DAL and not in the main executable. So I could not try a relative path approach.

What's the best solution to remove this absolute path? And how will you do it?

1 个答案:

答案 0 :(得分:0)

The argument to GetManifestResourceStream needs to be the formatted with the namespace, path in project and file name. For example:

"DAL.SomeProjectFolderForJsonData.Filename"

You should also ensure the Build Action of the json file is set to Embedded Resource.