0

A json file is stored in the Project folder.I want to deserialize data of that json document.Im getting error while fetching the path of json document.

I used this code and got error "An exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll but was not handled in user code"

        string file = HttpContext.Current.Server.MapPath("~/project.Services/xyz/myjsonfile.json");

            using (StreamReader reader = new StreamReader(file))
            {
              collection = JsonConvert.DeserializeObject<Dictionary<string, string>>(reader.ReadToEnd());             
           }
Soumika
  • 65
  • 1
  • 7
  • When I use MapPath(), I don't use any "~" (Tilde) char. Hence, you should be able to enter "project.Services..." into MapPath(). Also, try checking the value of the "file" string in your case to see if the path is correct. – Michael Mar 02 '17 at 15:06
  • Thanks Michael , but that also gave same error. – Soumika Mar 03 '17 at 06:47
  • Have you tried viewing the value of "file"? In Windows Forms, you can do "MessageBox.Show(file)". Otherwise, you can right-click on "using" and choose "Run to cursor", followed by mousing over "file" when the debugger gets to the cursor point during execution. One thing you forgot to include in your error message here is the exact line at which the error is generated. I am guessing "new StreamReader(file)". When viewing the code at time of error, double-click "Call Stack" items to see where the error happens. – Michael Mar 03 '17 at 14:07

1 Answers1

0

below code worked for me

            using (Stream stream = this.GetType().Assembly.GetManifestResourceStream(this.GetType().Assembly.GetName().Name + "." + filename))
         {using (StreamReader reader = new StreamReader(stream))
            {
                collection = JsonConvert.DeserializeObject<Dictionary<string, string>>(reader.ReadToEnd());
            }}

Thank You Michael

Soumika
  • 65
  • 1
  • 7