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