Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read from a JSON file inside a project

I have a directory named Resources in my WPF project and I have a Settings.json inside that directory. I want to read content from that file. In file settings I have Build Action -> Embedded Resource and Copy to Output Directory -> Copy Always And I read the file like this :

using (StreamReader r = new StreamReader(@"/Resources/Settings.json"))

And I get the following exception :

{"Could not find a part of the path 'C:\Resources\Settings.json'."}

How can I make this read the file in that directory? Thanks

like image 935
jason Avatar asked Aug 01 '14 13:08

jason


2 Answers

Since you've got your Build Action set to Embedded Resource, you probably want to use the Assembly.GetManifestResourceStream method.

For example:

using (Stream stream = assembly.GetManifestResourceStream("MyCompany.Namespace.Settings.json"))
using (StreamReader reader = new StreamReader(stream))
like image 87
Switchbreak Avatar answered Oct 13 '22 00:10

Switchbreak


using (StreamReader r = new StreamReader(Application.StartupPath + @"/Resources/Settings.json"))
like image 31
Sefa Avatar answered Oct 12 '22 22:10

Sefa