读取任意 文件目录,生成 json格式!

skydhx 2016-03-08 04:32:08
要求实现给一个磁实现盘路径,读出 磁盘的文件夹、文件名称,生存树形结构,

我目前的思路是 循环读取 生存json字符串, 已经实现部分, 多文件夹嵌套级别 我仍然没有实现

提供我现在的代码

int id=1;
int subId=1;
StringBuilder jsonStr=new StringBuilder();
Dictionary<string, int> pIdWith=new Dictionary<string, int>();
public string InitTree(string sourceFolder, int no)
{

string[] subPaths=Directory.GetDirectories(sourceFolder);//得到所有子目录
foreach( string path in subPaths )
{

jsonStr.Append("{ \"id\":\""+id+"\",\"pId\":\"0\",\"name\":\""+path.Replace(sourceFolder+"\\", "")+"\"},");

subId+=10001;

InitTree(path, id);
}

string[] files=Directory.GetFiles(sourceFolder);
foreach( string file in files )
{
subId++;
jsonStr.Append("{ \"id\":\""+subId+"\",\"pId\":\""+id+"\",\"name\":\""+file.Replace(sourceFolder+"\\", "")+"\",\"url\":\""+file+"\"},");
}
id++;
return jsonStr.ToString();
}
...全文
1057 15 打赏 收藏 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
咖啡猫-K 2017-10-23
  • 打赏
  • 举报
回复
有源代码么?
tanta 2016-03-09
  • 打赏
  • 举报
回复
1、定义树型json 2、递归文件填充
skydhx 2016-03-09
  • 打赏
  • 举报
回复
谢谢大伙的帮助,

我想要的格式就是 有个层级关系的 rutu如图
Justin-Liu 2016-03-09
  • 打赏
  • 举报
回复
引用 5 楼 crystal_lz 的回复:
[quote=引用 4 楼 Sky_Dhx 的回复:] 你生存的json 不是我想要的,我需要 得到 父级 的id 刚好与前台ztree 无缝对接!
不是叫你自己参考么 又没说我要帮你搞定 谁知道你说的id和前台是什么鬼[/quote]
Justin-Liu 2016-03-09
  • 打赏
  • 举报
回复
引用 5 楼 crystal_lz 的回复:
[quote=引用 4 楼 Sky_Dhx 的回复:] 你生存的json 不是我想要的,我需要 得到 父级 的id 刚好与前台ztree 无缝对接!
不是叫你自己参考么 又没说我要帮你搞定 谁知道你说的id和前台是什么鬼[/quote] 把他嘴掰开喂
skydhx 2016-03-09
  • 打赏
  • 举报
回复
终于自己实现了,功夫不负有心人,各位楼上的朋友谢谢鼎力相助! int id=1;//文件夹id int subId=10000; //文件id StringBuilder jsonStr=new StringBuilder(); Dictionary<string, int> pIdWith=new Dictionary<string, int>(); //存储文件夹路径 和文件夹id的字典 Dictionary<string, int> pIdNode=new Dictionary<string, int>(); //存储文件路径和文件夹id的字典 int pId=0; public string InitTree(string sourceFolder, int no) { if( subId==10000 ) { pIdWith.Add(sourceFolder, 0); pIdNode.Add(sourceFolder, 0); } string[] subPaths=Directory.GetDirectories(sourceFolder);//得到所有子目录 foreach( var item in subPaths ) { if( pId>0 ) { pIdWith.Add(item, pIdNode[sourceFolder]); } else { pIdWith.Add(item, pId); } } if( subPaths.Length>0 ) { pId++; id++; } foreach( string path in subPaths ) { string[] isFile=Directory.GetFiles(path); jsonStr.Append("{ \"id\":\""+id+"\",\"pId\":\""+(pIdWith[path])+"\",\"name\":\""+path.Replace(sourceFolder+"\\", "")+"\"},"); subId+=10001; pIdNode.Add(path, id); InitTree(path, id); } string[] files=Directory.GetFiles(sourceFolder); foreach( string file in files ) { subId++; jsonStr.Append("{ \"id\":\""+subId+"\",\"pId\":\""+pIdNode[sourceFolder]+"\",\"name\":\""+file.Replace(sourceFolder+"\\", "")+"\",\"url\":\""+file+"\"},"); } id++; return jsonStr.ToString(); }
BitCoffee 2016-03-09
  • 打赏
  • 举报
回复

public void GetTreeViewNode(ref List<TreeViewNode> list,string folder,ref int id)
{
    int pId = id;
    foreach (var dir in Directory.GetDirectories(folder)) 
    {
        TreeViewNode node = TreeViewNode();
        node.id = id + 1;
        node.pId = pId;
        node.name = dir.Remove(0,dir.LastIndeOf('\\"));
        node.url = dir;
        list.Add(node);
        id += 1;
        GetTreeViewNode(ref list,dir,ref id);
    }
 
    foreach(var file in Directory.GetFiles(sourceFolder))
    {
        TreeViewNode node = TreeViewNode();
        node.id = id + 1;
        node.pId = pId;
        node.name = dir.Remove(0,file.LastIndeOf('\\"));
        node.url = file;
        list.Add(node);
        id += 1;
    }
}
BitCoffee 2016-03-09
  • 打赏
  • 举报
回复

public class TreeViewNode
{
    public int id {get;set;}
    public int pId {get;set;}
    public string name {get;set;}
    public bool open {get;set;}
    public string iconOpen {get;set;}
    public string icon {get;set;}
    public string url {get;set;}
}

public string GetJsonTreeView(string sourceFolder)
{
    List<TreeViewNode> list = new List<TreeViewNode>();
    int id = 0;
    GetTreeViewNode(ref list,sourceFolder,ref id);
    return new JavaScriptSerializer().Serialize(list);
}

public void GetTreeViewNode(ref List<TreeViewNode> list,string folder,ref int id)
{
    foreach (var dir in Directory.GetDirectories(folder)) {
        TreeViewNode node = TreeViewNode();
        node.id = id + 1;
        node.pId = id;
        node.name = dir.Remove(0,dir.LastIndeOf('\\"));
        node.url = dir;
        list.Add(node);
        id += 1;
        GetTreeViewNode(ref list,dir,ref id);
    }

    foreach(var file in Directory.GetFiles(sourceFolder))
    {
        TreeViewNode node = TreeViewNode();
        node.id = id + 1;
        node.pId = id;
        node.name = dir.Remove(0,file.LastIndeOf('\\"));
        node.url = file;
        list.Add(node);
        id += 1;
    }
}
skydhx 2016-03-09
  • 打赏
  • 举报
回复
引用 9 楼 tanta 的回复:
1、定义树型json 2、递归文件填充
实在写不出来,自顶啊,看哪位高手能写出来
skydhx 2016-03-09
  • 打赏
  • 举报
回复
引用 9 楼 tanta 的回复:
1、定义树型json 2、递归文件填充
兄弟, 要不你试试,我写了一天了,都没折腾出来,还是不正确,如果多文件夹目录我的递归就乱套了,写不出来
crystal_lz 2016-03-08
  • 打赏
  • 举报
回复
引用 4 楼 Sky_Dhx 的回复:
你生存的json 不是我想要的,我需要 得到 父级 的id 刚好与前台ztree 无缝对接!
不是叫你自己参考么 又没说我要帮你搞定 谁知道你说的id和前台是什么鬼
skydhx 2016-03-08
  • 打赏
  • 举报
回复
引用 3 楼 crystal_lz 的回复:

textBox1.Text = GetJsonTree("F:\\backup\\WindowsFormsApplication4\\obj");

private string GetJsonTree(string strPath) {
    string strRet = "{{\"name\":\"{0}\",\"File\":[{2}],\"sub\":[{1}]}}";
    List<string> lst = new List<string>();
    string strFolderName = strPath.Substring(strPath.LastIndexOf('\\') + 1);
    string strSubFile = string.Empty;
    foreach (var v in Directory.GetFiles(strPath)) {
        lst.Add("\"" + Path.GetFileName(v) + "\"");
    }
    strSubFile = string.Join(",", lst.ToArray());
    lst.Clear();
    foreach (var v in Directory.GetDirectories(strPath)) {
        lst.Add(GetJsonTree(v));
    }
    return string.Format(strRet, strFolderName, string.Join(",", lst.ToArray()), strSubFile);
}

{
    "name": "obj",
    "File": [],
    "sub": [
        {
            "name": "Debug",
            "File": [
                "DesignTimeResolveAssemblyReferences.cache",
                "DesignTimeResolveAssemblyReferencesInput.cache",
                "WindowsFormsApplication4.csproj.FileListAbsolute.txt",
                "WindowsFormsApplication4.csproj.GenerateResource.Cache",
                "WindowsFormsApplication4.csproj.ResolveComReference.cache",
                "WindowsFormsApplication4.csprojResolveAssemblyReference.cache",
                "WindowsFormsApplication4.exe",
                "WindowsFormsApplication4.Form1.resources",
                "WindowsFormsApplication4.Form2.resources",
                "WindowsFormsApplication4.pdb",
                "WindowsFormsApplication4.Properties.Resources.resources"
            ],
            "sub": [
                {
                    "name": "TempPE",
                    "File": [],
                    "sub": []
                }
            ]
        },
        {
            "name": "Release",
            "File": [
                "DesignTimeResolveAssemblyReferences.cache",
                "DesignTimeResolveAssemblyReferencesInput.cache",
                "TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs",
                "TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs",
                "TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs",
                "WindowsFormsApplication4.csproj.FileListAbsolute.txt",
                "WindowsFormsApplication4.csproj.GenerateResource.Cache",
                "WindowsFormsApplication4.csproj.ResolveComReference.cache",
                "WindowsFormsApplication4.csprojResolveAssemblyReference.cache",
                "WindowsFormsApplication4.exe",
                "WindowsFormsApplication4.Form1.resources",
                "WindowsFormsApplication4.Form2.resources",
                "WindowsFormsApplication4.pdb",
                "WindowsFormsApplication4.Properties.Resources.resources"
            ],
            "sub": [
                {
                    "name": "TempPE",
                    "File": [
                        "Properties.Resources.Designer.cs.dll"
                    ],
                    "sub": []
                }
            ]
        }
    ]
}
临时帮你写了一个自己参考 拼接的时候 自己转义特殊字符
你生存的json 不是我想要的,我需要 得到 父级 的id 刚好与前台ztree 无缝对接!
crystal_lz 2016-03-08
  • 打赏
  • 举报
回复

textBox1.Text = GetJsonTree("F:\\backup\\WindowsFormsApplication4\\obj");

private string GetJsonTree(string strPath) {
    string strRet = "{{\"name\":\"{0}\",\"File\":[{2}],\"sub\":[{1}]}}";
    List<string> lst = new List<string>();
    string strFolderName = strPath.Substring(strPath.LastIndexOf('\\') + 1);
    string strSubFile = string.Empty;
    foreach (var v in Directory.GetFiles(strPath)) {
        lst.Add("\"" + Path.GetFileName(v) + "\"");
    }
    strSubFile = string.Join(",", lst.ToArray());
    lst.Clear();
    foreach (var v in Directory.GetDirectories(strPath)) {
        lst.Add(GetJsonTree(v));
    }
    return string.Format(strRet, strFolderName, string.Join(",", lst.ToArray()), strSubFile);
}

{
    "name": "obj",
    "File": [],
    "sub": [
        {
            "name": "Debug",
            "File": [
                "DesignTimeResolveAssemblyReferences.cache",
                "DesignTimeResolveAssemblyReferencesInput.cache",
                "WindowsFormsApplication4.csproj.FileListAbsolute.txt",
                "WindowsFormsApplication4.csproj.GenerateResource.Cache",
                "WindowsFormsApplication4.csproj.ResolveComReference.cache",
                "WindowsFormsApplication4.csprojResolveAssemblyReference.cache",
                "WindowsFormsApplication4.exe",
                "WindowsFormsApplication4.Form1.resources",
                "WindowsFormsApplication4.Form2.resources",
                "WindowsFormsApplication4.pdb",
                "WindowsFormsApplication4.Properties.Resources.resources"
            ],
            "sub": [
                {
                    "name": "TempPE",
                    "File": [],
                    "sub": []
                }
            ]
        },
        {
            "name": "Release",
            "File": [
                "DesignTimeResolveAssemblyReferences.cache",
                "DesignTimeResolveAssemblyReferencesInput.cache",
                "TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs",
                "TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs",
                "TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs",
                "WindowsFormsApplication4.csproj.FileListAbsolute.txt",
                "WindowsFormsApplication4.csproj.GenerateResource.Cache",
                "WindowsFormsApplication4.csproj.ResolveComReference.cache",
                "WindowsFormsApplication4.csprojResolveAssemblyReference.cache",
                "WindowsFormsApplication4.exe",
                "WindowsFormsApplication4.Form1.resources",
                "WindowsFormsApplication4.Form2.resources",
                "WindowsFormsApplication4.pdb",
                "WindowsFormsApplication4.Properties.Resources.resources"
            ],
            "sub": [
                {
                    "name": "TempPE",
                    "File": [
                        "Properties.Resources.Designer.cs.dll"
                    ],
                    "sub": []
                }
            ]
        }
    ]
}
临时帮你写了一个自己参考 拼接的时候 自己转义特殊字符
skydhx 2016-03-08
  • 打赏
  • 举报
回复
引用 1 楼 guwei4037 的回复:
自定义一个树形结构

class TreeNode
{
public int nodeid;
public string nodename;
public TreeNode parentNode;
}

然后读取磁盘路径,动态递归构造List<TreeNode>结构。

最后通过序列化工具序列化成json即可。



兄弟,目录结构的同时还有文件名的读取!我代码写不出来,脑袋比较笨拙、求源代码、




全栈极简 2016-03-08
  • 打赏
  • 举报
回复
自定义一个树形结构 class TreeNode { public int nodeid; public string nodename; public TreeNode parentNode; } 然后读取磁盘路径,动态递归构造List<TreeNode>结构。 最后通过序列化工具序列化成json即可。

111,128

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧