0

I want to read the json file data in my application, file is in D:\Projects\Trans\API, while debugging I'm getting the error:

Could not find file 'D:\projects\Trans\API\bin\Debug\alert.json'.

using Newtonsoft.Json;

string json = File.ReadAllText("alert.json");
dynamic jsonObj = JsonConvert.DeserializeObject(json);
jsonObj[0] = "1567629797772";
string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
File.WriteAllText("alert.json", output);

Json data:

{
    "36c": "1567629797772",
    "36a": "1567675661847",
    "369": "1567669087138",
    "36d": "1567664290851",
    "36e": "1567675343047"
}
theduck
  • 2,589
  • 13
  • 17
  • 23
  • 1
    Where is your file? what is the location of it? is it included in your project? if Yes, Did you set `Copy to Output Directory` -> `Copy Always` – Prasad Telkikar Sep 06 '19 at 07:31
  • Possible duplicate of [Could not find file ..\bin\Debug\content.dat](https://stackoverflow.com/questions/3318149/could-not-find-file-bin-debug-content-dat) – xdtTransform Sep 06 '19 at 07:32

3 Answers3

2

Unless the path is explicitly defined for following line,

File.ReadAllText("alert.json");

It will refer 'D:\projects\Trans\API\bin\Debug\alert.json' on Debug mode and

'D:\projects\Trans\API\bin\Release\alert.json' on Release mode.

Change it to File.ReadAllText(@"D:\projects\Trans\API\alert.json");

You can try to make it better by using any following methods,

  1. Use following methods to get current working directory and place the json file there.

    System.IO.Path.GetDirectoryName(Application.ExecutablePath); System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

  2. Copy json file during installation to any special folder like program files and access using any of the following code,

    Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);

Matt
  • 1,953
  • 1
  • 19
  • 42
  • This will work only inside VS. If the user takes the application on another computer (as you should be expected to do when you finish your work) this solution will probably fail again unless you create the same folder structure on another computer – Steve Sep 06 '19 at 07:34
  • @PrasadTelkikar: Neither will your comment. Please explain what is wrong with the answer. We are all here to learn. – Palle Due Sep 06 '19 at 08:00
  • As @Steve said, this will fail if your colleagues is using `C:` drive to store local repository – Prasad Telkikar Sep 06 '19 at 08:00
  • @Palle Due, We have other ways to read path, passing hard coded string is not a good practice here – Prasad Telkikar Sep 06 '19 at 08:05
  • @PrasadTelkikar: I agree, and now it's clear to everyone. Thanks. – Palle Due Sep 06 '19 at 08:11
1

Put your file into the correct folder, which is the folder any new class gets put into. So in the folder that has ".vs", [FOLDER] "project name" and "projectName.sln", you want to put it inside [FOLDER] "project name". Then, drag the item into your visual studio class overview (by default on the right hand side). Then click the file in visual studio, so you see the little properties menu. There, at "Copy to output directory", choose "Copy if newer". Now you can just read the file like this, without all the rubbish that is computer-specific

string json = File.ReadAllText("alert.json");
Luuk
  • 11
  • 1
0

You can find the current working directory with Environment.CurrentDirectory (https://learn.microsoft.com/en-us/dotnet/api/system.environment.currentdirectory?view=netframework-4.8)

To access the direct path in D:\Projects\Trans\API, specify the entire file path, so that it doesn't matter where you run the .exe from.

Luke Parker
  • 299
  • 1
  • 10