遍历文件夹及文件名的保存

public partial class WebForm1 : System.Web.UI.Page
    {
        static string strConn = ConfigurationManager.ConnectionStrings["tabll"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //提交按钮单击事件
        protected void Button1_Click(object sender, EventArgs e)
        {
            string path = this.TextBox1.Text.Trim();
            InputBaseInfo(path);
        }

        //录入顶级父目录
        private void InputBaseInfo(string path)
        {
            using (SqlConnection conn = new SqlConnection(strConn))
            {
                string name = Path.GetFileNameWithoutExtension(path);
                string str = string.Format("insert into fromTable(URL,ZIID)  values ('{0}',{1})", name, 0);
                SqlCommand command = new SqlCommand(str, conn);
                conn.Open();
                command.ExecuteNonQuery();
            }
            InputSonInfos(path);
        }

        //插入子目录和文件下
        private bool InputSonInfos(string path)
        {
            //通过路径得到子目录和文件
            string[] directoryInfos = Directory.GetDirectories(path);
            string[] fileInfos = Directory.GetFiles(path);
            //遍历子文件夹
            if (fileInfos != null)
            {
                shoe(path);
            }
            foreach (var files in fileInfos)
            {
                int ID = SelectID(path);
            }

            //判断是否存在子文件夹
            if (directoryInfos == null || directoryInfos.Length == 0)
            {
                return false;
            }
            //存储文件夹遍历所有子文件夹
            foreach (var dire in directoryInfos)
            {
                int baseID = 0;
                string stename = dire;
                string strname = Path.GetFileName(stename);
                string pathname = Path.GetFileName(path);
                string strSql = "select ID from fromTable where URL='" + pathname + "'";
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    SqlCommand cmd = new SqlCommand(strSql, conn);
                    conn.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        baseID = Convert.ToInt32(reader[0]);
                    }
                }
                string strSql1 = string.Format("insert into fromTable(URL,ZIID) values('{0}',{1})", strname, baseID);
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    SqlCommand cmd = new SqlCommand(strSql1, conn);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
                //调用递归函数添加子文件
                if (!InputSonInfos(dire))
                {
                    continue;
                }
                continue;
            }
            return true;
        }

        //向数据库中添加根目录的路径及ID
        private static int SelectID(string path)
        {
            int ID = 0;
            string strname = Path.GetFileNameWithoutExtension(path);
            string sql = "select ID from fromTable where URL='" + strname + "'";
            using (SqlConnection conn = new SqlConnection(strConn))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    ID = Convert.ToInt32(reader[0]);
                }
            }
            return ID;
        }
        //向数据库中添加子目录的路径及上级的ID
        private static void InsertPath(string path, int BaseID)
        {
            string pathname = Path.GetFileName(path);
            string sql = string.Format("insert into fromTable(URL,ZIID) values ('{0}',{1})", pathname, BaseID);
            using (SqlConnection conn = new SqlConnection(strConn))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }
        //获取文件名
        private static void shoe(string path)
        {
            DirectoryInfo dirinfo = new DirectoryInfo(path);
            FileInfo[] fileinfo = dirinfo.GetFiles();
            foreach (FileInfo file in fileinfo)
            {
                string str = file.FullName.ToString();
                string stdname = Path.GetFileName(str);
                string pathname = Path.GetFileName(path);
                SelectID(path);
                int baseID = 0;
                string strSql = "select ID from fromTable where URL='" + pathname + "'";
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    SqlCommand cmd = new SqlCommand(strSql, conn);
                    conn.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        baseID = Convert.ToInt32(reader[0]);
                    }
                }
                string strSql1 = string.Format("insert into fromTable(URL,ZIID) values('{0}',{1})", stdname, baseID);
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    SqlCommand cmd = new SqlCommand(strSql1, conn);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
            }
        }
       
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值