循环遍历文件夹分级目录 插入数据库

gongjie416 2013-11-20 10:51:40
目前想要实现的效果是想在遍历文件夹时 根据文件夹,子文件夹生成 目录数据并插入到数据库,不过现在数据库规定的目录规则看的实在 头大,实在不知道如何写请大家帮帮忙
 
public void FindFile(string dirPath) //参数dirPath为指定的目录
{
string ab = "";
DirectoryInfo Dir = new DirectoryInfo(dirPath);
foreach (DirectoryInfo d in Dir.GetDirectories())
{
string fname=ab+">"+d.ToString();
string sql = "insert into testFile(filename)values('" + fname + "')";
SqlHelper s = new SqlHelper();
s.Db_Operate(sql);
FindFile(Dir + "\\" + d.ToString());
string sql2 = "select @@identity from testFile";
if (d.GetDirectories().Length > 0)
{
ab = s.Db_ReadField(sql2).ToString() + ":" + d.ToString() ;
}
ab = s.Db_ReadField(sql2).ToString()+":"+d.ToString ()+"<";
}
}



想要实现插入到filename这个字段格式 1级目录为 <语文
2级目录为 1:语言<人教版 (1为语文刚入时取得的ID)
3级目录为 1:语文>7:人教版<一年级上 (数字都是该条插入时的ID)
4级目录为 1:语文>7:人教版>188:一年级上<本册综合



...全文
271 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
敌敌畏耶 2013-11-20
  • 打赏
  • 举报
回复
递归?
wyufen 2013-11-20
  • 打赏
  • 举报
回复
用递归吧……
Teln_小凯 2013-11-20
  • 打赏
  • 举报
回复
我这个也不全对,主要就是在for循环内部的控制和获取了,思路大概这个样子。。。。汗一个
Teln_小凯 2013-11-20
  • 打赏
  • 举报
回复
对了 获取刚刚插入的ID是@@IDENTITY
Teln_小凯 2013-11-20
  • 打赏
  • 举报
回复
大概这个样子
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int id = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //递归加载D盘下的所有目录
            GetAllDirList("D:\\");
        }
        
        public void GetAllDirList(string strBaseDir)
        {
            DirectoryInfo di = new DirectoryInfo(strBaseDir);
            DirectoryInfo[] diA = di.GetDirectories();
            for (int i = 0; i < diA.Length; i++)
            {
                //本来想把父目录做参数的,但是有点伤脑筋,所以用简单的了
                //比如D盘第一个目录是  语文
                string name = "";
                if (id == 0)
                {
                    name = diA[i].Name;
                }
                else
                { 
                    //根据id获取到他的名字
                    //name=id : (select name from table where id=id)< diA[i].Name;
                }
                //写个插入方法insert into values(name)
                //然后获取刚刚插入的ID好像是@@select...什么的 忘记了 反正就是获取刚刚插入的ID
                //id=刚刚插入的ID
                //下面开始递归
                GetAllDirList(diA[i].FullName);
            }
        }

    }
}
具体的自己调试调试就差不多了
gongjie416 2013-11-20
  • 打赏
  • 举报
回复
也不知道哪个混蛋设计的这种目录树格式,把我坑苦了,
gongjie416 2013-11-20
  • 打赏
  • 举报
回复
有试过这样用,但出来的格式跟需求的相差很大
gongjie416 2013-11-20
  • 打赏
  • 举报
回复
有试过这样用,但出来的格式跟需求的相关很大
Teln_小凯 2013-11-20
  • 打赏
  • 举报
回复
貌似要用到递归,才好确定谁是谁的子,然后目录名才好拼接
bdmh 2013-11-20
  • 打赏
  • 举报
回复
遍历时,你需要记录一个父级目录的名称,这样后续的子目录才知道属于那个父级目录,这个变量可以作为参数 FindFile(string dirPath,string parentdir) { ... 先获得父目录 FindFile(Dir + "\\" + d.ToString(),父目录); ... }
gongjie416 2013-11-20
  • 打赏
  • 举报
回复
哪个大哥帮帮忙,头都大了,也没弄清楚
gongjie416 2013-11-20
  • 打赏
  • 举报
回复
谢谢,非常感谢
feiniao19830822 2013-11-20
  • 打赏
  • 举报
回复
引用 13 楼 gongjie416 的回复:
谢谢,虽然还有些小瑕丝 但已经是最接近我的需求了
看14楼的修改
feiniao19830822 2013-11-20
  • 打赏
  • 举报
回复
如果把“f:\语文”下面一级的的3个目录 当作一级目录. 这样改一下就行了。

        private void Form1_Load(object sender, EventArgs e)
        {
            string sParentPath = "";
            DirectoryInfo dir = new DirectoryInfo(@"F:\语文");
            foreach (DirectoryInfo d in dir.GetDirectories())
            {
                DirInsert(d.FullName,sParentPath);
            }
        }
 
        private void DirInsert(string dirFullPath, string sParentPath)
        {
            DirectoryInfo dir = new DirectoryInfo(dirFullPath);
            string dirPath = dir.Name;
 
            //先把当前目录插入
            string fname = sParentPath + "<" + dirPath;
            string sql = "insert into testFile(filename)values('" + fname + "')";
            SqlHelper s = new SqlHelper();
            s.Db_Operate(sql);
            string sql2 = "select @@identity from testFile";
            string IdCur = s.Db_ReadField(sql2).ToString();
 
            string sParentPathNew = "";
            if (string.IsNullOrEmpty(sParentPath))
            {
                //一级目录
                sParentPathNew = string.Format("{0}:{1}", IdCur, dirPath);
            }
            else
            {
                sParentPathNew = string.Format("{0}>{1}:{2}", sParentPath, IdCur, dirPath);
            }
 
            //递归
            DirectoryInfo Dir = new DirectoryInfo(dirFullPath);
            foreach (DirectoryInfo d in Dir.GetDirectories())
            {
                DirInsert(d.FullName,sParentPathNew);
            }
        }
gongjie416 2013-11-20
  • 打赏
  • 举报
回复
谢谢,虽然还有些小瑕丝 但已经是最接近我的需求了
feiniao19830822 2013-11-20
  • 打赏
  • 举报
回复

        private void Form1_Load(object sender, EventArgs e)
        {
            string sParentPath = "";
            DirInsert(@"F:\语文", sParentPath);
        }

        private void DirInsert(string dirFullPath, string sParentPath)
        {
            DirectoryInfo dir = new DirectoryInfo(dirFullPath);
            string dirPath = dir.Name;

            //先把当前目录插入
            string fname = sParentPath + "<" + dirPath;
            string sql = "insert into testFile(filename)values('" + fname + "')";
            SqlHelper s = new SqlHelper();
            s.Db_Operate(sql);
            string sql2 = "select @@identity from testFile";
            string IdCur = s.Db_ReadField(sql2).ToString();

            string sParentPathNew = "";
            if (string.IsNullOrEmpty(sParentPath))
            {
                //一级目录
                sParentPathNew = string.Format("{0}:{1}", IdCur, dirPath);
            }
            else
            {
                sParentPathNew = string.Format("{0}>{1}:{2}", sParentPath, IdCur, dirPath);
            }

            //递归
            DirectoryInfo Dir = new DirectoryInfo(dirFullPath);
            foreach (DirectoryInfo d in Dir.GetDirectories())
            {
                DirInsert(d.FullName,sParentPathNew);
            }
        }

110,534

社区成员

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

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

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