gpt4 book ai didi

c# - 在 JSON 中保存目录路径?

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:27 24 4
gpt4 key购买 nike

我有一个显示文件夹 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 文件是个好主意。正确的?我正在使用 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称为 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();
}

保存函数:

    public void saveData()
{
JsonSerializer serializer = new JsonSerializer();
using (StreamWriter sw = new StreamWriter(appData))
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, syncedDirectories);
}
}

当我将数据保存到文件中时,我将如何遍历我的 JSON 对象并将其保存回 List<SavedData>为了添加/删除数据?

更新 1

我已经弄清楚如何处理我的数据(如下),但我该如何将这些“节点”添加到 TreeView 中呢?

    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;
}

以后打算把这个程序开源到GitHub上。当我这样做时,我会在此处发布链接。

最佳答案

在研究了一个函数几个小时后,我意识到需要两个函数才能完成这个任务。

    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;
}

这会获取主目录,然后循环查找所有子目录并将它们添加到我的列表中。

关于c# - 在 JSON 中保存目录路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26456941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com