Search code examples
c#.netjsonjson.netstreamreader

Load in an unknown number of files in a specific directory


I'm trying to load an unknown number of json files in a directory using json.net (C:/users/Nathan/Documents/test)

I want to be able to add json files to this directory and no matter how many there are my program should be able to access all of them and load them as separate a jObject with unique names.

Is this even possible?

Edit I don't have code yet, I have to get ideas what direction to take for this.


Solution

  • Code would be something like below:

    var fileNames = Directory.GetFiles(@"C:\user\Nathan\Documents\test");
    
    foreach(var file in fileNames)
    { 
      using (var sr = File.OpenText(file))
      using (var reader = new JsonTextReader(sr))
      {
        var json = (JObject) JToken.ReadFrom(reader);
      }
    }