0

I have a file directory in C:Data\ like

enter image description here

How can I write on an existing JSON file called Offline.json to be like:

{
"GDB":[ "C:Data/GDB/GDB1.gdb",
        "C:Data/GDB/GDB2.gdb", 
        "C:Data/GDB/GDB3.gdb" 
       ],
"TPK":[ "C:Data/TPK/TPK1.gdb",
        "C:Data/TPK/TPK2.gdb"
       ],
"MPK":[ "C:Data/MPK/MPK1.mpk",
        "C:Data/MPK/MPK2.mpk", 
        "C:Data/MPK/MPK3.mpk",
        "C:Data/MPK/MPK4.mpk",
        "C:Data/MPK/MPK5.mpk", 
        "C:Data/MPK/MPK6.mpk" 
       ]
}

on loading the WPF main windows?

1 Answer 1

1

Using Newtonsoft Json.NET this is kinda easy to accomplish:

// Your main code...
String path = @"C:\Data\";
DirectoryInfo di = new DirectoryInfo(path);
String json = GetJsonDirectoryStructure(di).ToString();

// Method implementation...
public static JObject GetJsonDirectoryStructure(DirectoryInfo di)
{
    JObject jObj = new JObject();

    foreach (DirectoryInfo diChild in di.GetDirectories())
        jObj.Add(diChild.Name, GetJsonDirectoryStructure(diChild));

    foreach (FileInfo di in di.GetFiles())
        jObj.Add(fi.Name, JValue.CreateNull());

    return jObj;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.