110,537
社区成员
发帖
与我相关
我的任务
分享
class cDir
{
private string strCn = "Data Source=.;Initial Catalog=DFInfo;Persist Security Info=True;User ID=DFInfo;pwd=DFInfo";
public string strPath;
public cDir(string strPath)
{
this.strPath = strPath;
}
public void funDir()
{
//遍历文件夹
string[] strDirPath = Directory.GetDirectories(strPath);
string[] strFilePath = Directory.GetFiles(strPath);
foreach (string strDPath in strDirPath)
{
cDir cD = new cDir(strDPath);
//如果有子文件夹的话,另起一个线程,对子文件夹遍历
Thread initDFDB = new Thread(new ThreadStart(cD.funDir));
initDFDB.Start();
}
foreach (string strFPath in strFilePath)
{
FileInfo fi = new FileInfo(strFPath);
string strFileName = fi.Name;
string strCreationTime = fi.CreationTime.ToString();
string strLastWriteTime = fi.LastWriteTime.ToString();
long lLength = fi.Length;
initDB(strPath, strFileName, strCreationTime, strLastWriteTime, lLength);
}
return;
}
private void initDB(string strPath, string strFileName, string strCreationTime, string strLastWriteTime, long lLength)
{
//加入新数据
string strSql = "insert into tbFile (Path,FileName,CreationTime,LastWriteTime,lLength) values (@Path,@FileName,@CreationTime,@LastWriteTime,@lLength)";
SqlParameter[] para = { new SqlParameter("@Path", strPath), new SqlParameter("@FileName", strFileName), new SqlParameter("@CreationTime", strCreationTime), new SqlParameter("@LastWriteTime", strLastWriteTime), new SqlParameter("@lLength", lLength) };
exeSql(strSql, para);
//写入数据库完毕
}
private void exeSql(string strSql, SqlParameter[] para)
{
SqlConnection sc = new SqlConnection(strCn);
SqlCommand scmd = new SqlCommand(strSql, sc);
foreach (SqlParameter sp in para)
{
scmd.Parameters.Add(sp);
}
sc.Open();
scmd.ExecuteNonQuery();
scmd.Parameters.Clear();
sc.Close();
sc.Dispose();
}
}
class cCount
{
public int intCount;
public cCount(int intCount)
{
this.intCount = intCount;
}
public void add()
{
intCount++;
return;
}
public void minus()
{
intCount--;
return;
}
}
private void funDir(string strPath)
{
string[] strDirPath = Directory.GetDirectories(strPath);
string[] strFilePath = Directory.GetFiles(strPath);
foreach (string strDPath in strDirPath)
{
this.funDir(strDPath);
}
foreach (string strFPath in strFilePath)
{
FileInfo fi = new FileInfo(strFPath);
string strFileName = fi.Name;
string strCreationTime = fi.CreationTime.ToString();
string strLastWriteTime = fi.LastWriteTime.ToString();
long lLength = fi.Length;
initDB(strPath, strFileName, strCreationTime, strLastWriteTime, lLength);
}
return;
}