在JSON中保存目录路径?

时间:2014-10-20 00:27:59

标签: c# json wpf treeview

我有一个显示文件夹树视图的应用程序,格式如下:

- Main Folder
    - SubFolder
    - SubFolder
        - SubFolder
    - SubFolder

问题: 1)How do I search for all the folders (not files) and add them to a List called syncDirectories (已修复)

2)How would I then iterate through my JSON object and save it back to List<SavedData> in order to add/remove data (已修复)

3)How would I loop through my syncDirectories List and add all the nodes to the TreeView in WPF. (Keep in mind the treeview with checkbox class i am using.

等等。此外,这是演示格式的示例,用户可以拥有更多子目录。

由于这个TREEVIEW CLASS.

,每个文件夹都有一个复选框

为了保存用户添加(和检查)的所有文件夹和子文件夹,我想保存列出所有文件夹和子文件夹路径的JSON文件,以及它们的(true / false)检查值是个好主意。对?我正在使用Json.NET。

用一些JSON搞砸了,我提出了这种格式:

{   
    "path": "path/here",
    "subDirectories": [
        {"path": "sub/path/here","sync": false},
        {"path": "sub/path/here","sync": true},
        {"path": "sub/path/here","sync": false}
    ]
}

其中翻译成班级:

public class SavedData
{
    public string path { get; set; }
    public List<SubDirectory> subDirectories { get; set; }
}

public class SubDirectory
{
    public string path { get; set; }
    public bool sync { get; set; }
    public List<SubDirectory> subDirectories { get; set; }
}

这提出了一个问题,如何搜索所有文件夹(而不是文件)并将其添加到名为List的{​​{1}}?

以下是我目前在syncDirectories函数中的内容:

addDirectory

保存功能:

private void addDirectory()
{
    var dialog = new FolderBrowserDialog();
    DialogResult result = dialog.ShowDialog();
    if (result == System.Windows.Forms.DialogResult.OK)
    {
        SavedData data = new SavedData();
        data.path = dialog.SelectedPath;
        syncedDirectories.Add();
    }
    // Add the data to syncedDirectories List, then save the list and refresh the treeView
    saveData();
}

当我将数据保存到文件中时,如何迭代我的JSON对象并将其保存回 public void saveData() { JsonSerializer serializer = new JsonSerializer(); using (StreamWriter sw = new StreamWriter(appData)) using (JsonWriter writer = new JsonTextWriter(sw)) { serializer.Serialize(writer, syncedDirectories); } } 以添加/删除数据?

更新1

我已经找到了如何处理我的数据(下图),但我究竟会如何将这些“节点”添加到树视图中呢?

List<SavedData>

稍后,我打算在GitHub上发布这个程序开源。当我这样做时,我会在这里发布链接。

1 个答案:

答案 0 :(得分:1)

在玩了几个小时的功能后,我意识到完成这项任务需要两个功能。

    public void getDirectories(string dir)
    {
        // Create the Data Object
        SavedData data = new SavedData();
        data.path = dir;
        data.subDirectories = new List<SubDirectory>();
        foreach (string directory in Directory.GetDirectories(dir))
        {
            SubDirectory subDir = new SubDirectory();
            subDir.path = directory;
            subDir.subDirectories = getSubDir(directory);
            data.subDirectories.Add(subDir);
        }
        syncedDirectories.Add(data);
    }

    private List<SubDirectory> getSubDir(string dir)
    {
        List<SubDirectory> dataList = new List<SubDirectory>();
        SubDirectory subDir = new SubDirectory();
        foreach (string directory in Directory.GetDirectories(dir))
        {
            subDir.path = directory;
            subDir.subDirectories = new List<SubDirectory>();
            subDir.subDirectories = getSubDir(directory);
            dataList.Add(subDir);
        }
        return dataList;
    }

这会使主目录循环并找到所有子目录并将它们添加到我的列表中。