110,534
社区成员
发帖
与我相关
我的任务
分享
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 ()+"<";
}
}
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);
}
}
}
}
具体的自己调试调试就差不多了
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);
}
}
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);
}
}