遍歷文件夾結構存儲到數據庫

using System;
using System.Web;
using System.Linq;
using System.Configuration;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.IO;
using System.Data.Common;
using System.Data;
using System.Security.Cryptography;
using System.Text;//有一些沒用到sql

方法一:數據庫

case "oldfiles"://老方法循環遍歷文件夾,缺點:文件夾層級數固定。
{
string sql = "";
var Pid = "0";
var Pid0 = "";
var Pid1 = "";
var Pid2 = "";
var Pid3 = "";
var Pid4 = "";
sql = "delete from table_files";
pghelper.ExecuteNonQuery(CommandType.Text, sql);
string fileNameDir = System.Web.HttpContext.Current.Server.MapPath("~");//項目路徑
string photo_path = fileNameDir + "resource/目標文件夾/";//文件路徑
DirectoryInfo theFolder = new DirectoryInfo(photo_path);//初始化對象,建立路徑
int cp = photo_path.IndexOf("resource");
photo_path = photo_path.Substring(cp);
string filenameP = "目標文件夾";
string filepathP = photo_path;
sql = "insert into table_files(pid,name,filepath) values('" + Pid + "', '" + filenameP + "','" + filepathP + "') RETURNING id;";
Pid = pghelper.InsertReturnId(sql).ToString();
FileInfo[] thefileInfo = theFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly);//獲取路徑photo_path的文件(.後綴文件)
foreach (FileInfo NextFile in thefileInfo) //遍歷根節點下文件
{
string filename = NextFile.Name;
string filepath = NextFile.FullName.Substring(cp);
sql = "insert into table_files(pid,name,filepath) values('" + Pid + "', '" + filename + "','" + filepath + "') RETURNING id;";
pghelper.InsertReturnId(sql);函數

}對象

DirectoryInfo[] dirInfo0 = theFolder.GetDirectories();//遍歷根節點文件夾
//判斷第一層是否存在文件夾
if (dirInfo0.Length > 0)
{
foreach (DirectoryInfo NextFolder0 in dirInfo0)
{
string filename0 = NextFolder0.Name;
string filepath0 = NextFolder0.FullName.Substring(cp);
sql = "insert into table_files(pid,name,filepath) values('" + Pid + "', '" + filename0 + "','" + filepath0 + "') RETURNING id;";
Pid0 = pghelper.InsertReturnId(sql).ToString();遞歸

FileInfo[] fileInfo0 = NextFolder0.GetFiles("*.*", SearchOption.TopDirectoryOnly);//獲取目錄dirInfo0的文件(.後綴文件)
foreach (FileInfo NextFile0 in fileInfo0) //遍歷第一層文件夾下的文件
{
string filePname00 = NextFile0.Name;
string filePpath00 = NextFile0.FullName.Substring(cp);
sql = "insert into table_files(pid,name,filepath) values('" + Pid0 + "', '" + filePname00 + "','" + filePpath00 + "') RETURNING id;";
pghelper.InsertReturnId(sql); ;
}string

DirectoryInfo[] dirInfo1 = NextFolder0.GetDirectories();//遍歷第一層文件夾
//判斷第二層是否存在文件夾
if (dirInfo1.Length > 0)
{
foreach (DirectoryInfo NextFolder1 in dirInfo1)
{it

string filename1 = NextFolder1.Name;
string filepath1 = NextFolder1.FullName.Substring(cp);
sql = "insert into table_files(pid,name,filepath) values('" + Pid0 + "', '" + filename1 + "','" + filepath1 + "') RETURNING id;";
Pid1 = pghelper.InsertReturnId(sql).ToString();
FileInfo[] fileInfo1 = NextFolder1.GetFiles("*.*", SearchOption.TopDirectoryOnly);//獲取目錄dirInfo1的文件(.後綴文件)
foreach (FileInfo NextFile1 in fileInfo1) //遍歷第二層文件夾下的文件
{io


string filename11 = NextFile1.Name;
string filepath11 = NextFile1.FullName.Substring(cp);
sql = "insert into table_files(pid,name,filepath) values('" + Pid1 + "', '" + filename11 + "','" + filepath11 + "') RETURNING id;";
pghelper.InsertReturnId(sql);
}
//NextFile.Name;
DirectoryInfo[] dirInfo2 = NextFolder1.GetDirectories();//遍歷第二層文件夾
//判斷第三層是否存在文件夾
if (dirInfo2.Length > 0)
{
foreach (DirectoryInfo NextFolder2 in dirInfo2)
{
string filename2 = NextFolder2.Name;
string filepath2 = NextFolder2.FullName.Substring(cp);
sql = "insert into table_files(pid,name,filepath) values('" + Pid1 + "', '" + filename2 + "','" + filepath2 + "') RETURNING id;";
Pid2 = pghelper.InsertReturnId(sql).ToString();
FileInfo[] fileInfo2 = NextFolder2.GetFiles("*.*", SearchOption.TopDirectoryOnly);//獲取目錄dirInfo2的文件(.後綴文件)
foreach (FileInfo NextFile2 in fileInfo2) //遍歷第三層文件夾下的文件
{
string filename22 = NextFile2.Name;
string filepath22 = NextFile2.FullName.Substring(cp);
sql = "insert into table_files(pid,name,filepath) values('" + Pid2 + "', '" + filename22 + "','" + filepath22 + "') RETURNING id;";
pghelper.InsertReturnId(sql);
}
//NextFile.Name;
DirectoryInfo[] dirInfo3 = NextFolder2.GetDirectories();//遍歷第三層文件夾
//判斷第四層是否存在文件夾
if (dirInfo3.Length > 0)
{
foreach (DirectoryInfo NextFolder3 in dirInfo3)
{
string filename3 = NextFolder3.Name;
string filepath3 = NextFolder3.FullName.Substring(cp);
sql = "insert into table_files(pid,name,filepath) values('" + Pid2 + "', '" + filename3 + "','" + filepath3 + "') RETURNING id;";
Pid3 = pghelper.InsertReturnId(sql).ToString();
FileInfo[] fileInfo3 = NextFolder3.GetFiles("*.*", SearchOption.TopDirectoryOnly);//獲取目錄dirInfo3的文件(.後綴文件)
foreach (FileInfo NextFile3 in fileInfo3) //遍歷第四層文件夾下的文件
{
string filename33 = NextFile3.Name;
string filepath33 = NextFile3.FullName.Substring(cp);
sql = "insert into table_files(pid,name,filepath) values('" + Pid3 + "', '" + filename33 + "','" + filepath33 + "') RETURNING id;";
pghelper.InsertReturnId(sql);
}
//NextFile.Name;
DirectoryInfo[] dirInfo4 = NextFolder3.GetDirectories();//遍歷第四層文件夾
//判斷第五層是否存在文件夾
if (dirInfo4.Length > 0)
{
foreach (DirectoryInfo NextFolder4 in dirInfo3)
{
string filename4 = NextFolder4.Name;
string filepath4 = NextFolder4.FullName.Substring(cp);
sql = "insert into table_files(pid,name,filepath) values('" + Pid3 + "', '" + filename4 + "','" + filepath4 + "') RETURNING id;";
Pid4 = pghelper.InsertReturnId(sql).ToString();
FileInfo[] fileInfo4 = NextFolder3.GetFiles("*.*", SearchOption.TopDirectoryOnly);//獲取目錄dirInfo4的文件(.後綴文件)
foreach (FileInfo NextFile4 in fileInfo4) //遍歷第五層文件夾下的文件
{
string filename44 = NextFile4.Name;
string filepath44 = NextFile4.FullName.Substring(cp);
sql = "insert into table_files(pid,name,filepath) values('" + Pid4 + "', '" + filename44 + "','" + filepath44 + "') RETURNING id;";
pghelper.InsertReturnId(sql);
}
//NextFile.Name;
DirectoryInfo[] dirInfo5 = NextFolder3.GetDirectories();//遍歷第五層文件夾
//判斷第六層是否存在文件夾
}
}
}
}
}
}
}
}
}
}table


}
break;foreach

 

方法二:

case "files"://新方法循環遍歷文件夾,在此方法中用到遞歸函數,文件夾層級數無限制
{
string sql = "";
var Pid = "0";
sql = "delete from table_files";
pghelper.ExecuteNonQuery(CommandType.Text, sql);
string fileNameDir = System.Web.HttpContext.Current.Server.MapPath("~");//項目路徑
string photo_path = fileNameDir + "resource/目標文件夾/";//文件路徑
DirectoryInfo theFolder = new DirectoryInfo(photo_path);//初始化對象,建立路徑
int cp = photo_path.IndexOf("resource");
photo_path = photo_path.Substring(cp);
string filenameP = "目標文件夾";
string filepathP = photo_path;
sql = "insert into table_files(pid,name,filepath) values('" + Pid + "', '" + filenameP + "','" + filepathP + "') RETURNING id;";
Pid = pghelper.InsertReturnId(sql).ToString();
FileInfo[] thefileInfo = theFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly);//獲取路徑photo_path的文件(.後綴文件)
foreach (FileInfo NextFile in thefileInfo) //遍歷根節點下文件
{
string filename = NextFile.Name;
string filepath = NextFile.FullName.Substring(cp);
sql = "insert into table_files(pid,name,filepath) values('" + Pid + "', '" + filename + "','" + filepath + "') RETURNING id;";
pghelper.InsertReturnId(sql);

}
DirectoryInfo[] dirInfo0 = theFolder.GetDirectories();//遍歷根節點文件夾
if (dirInfo0.Length > 0)
{
savefiles(dirInfo0, Pid, cp);
}

break;

 

//遍歷文件夾以及文件存入數據庫遞歸方法
public void savefiles(DirectoryInfo[] theFolder, string Pid, int cp)
{
string conn = ConfigurationManager.ConnectionStrings["PgDBConnection"].ConnectionString;
PostgreHelper pghelper = new PostgreHelper(conn);
string sql = "";
string _Pid = "";
foreach (DirectoryInfo NextFolder0 in theFolder)
{
string filename0 = NextFolder0.Name;
string filepath0 = NextFolder0.FullName.Substring(cp);
sql = "insert into table_files(pid,name,filepath) values('" + Pid + "', '" + filename0 + "','" + filepath0 + "') RETURNING id;";
_Pid = pghelper.InsertReturnId(sql).ToString();

FileInfo[] fileInfo0 = NextFolder0.GetFiles("*.*", SearchOption.TopDirectoryOnly);//獲取目錄dirInfo0的文件(.後綴文件)
foreach (FileInfo NextFile0 in fileInfo0) //遍歷文件夾下的文件
{
string filePname00 = NextFile0.Name;
string filePpath00 = NextFile0.FullName.Substring(cp);
sql = "insert into table_files(pid,name,filepath) values('" + _Pid + "', '" + filePname00 + "','" + filePpath00 + "') RETURNING id;";
pghelper.InsertReturnId(sql); ;
}

DirectoryInfo[] dirInfo1 = NextFolder0.GetDirectories();//遍歷文件夾 if (dirInfo1.Length > 0) { savefiles(dirInfo1, _Pid, cp);//執行遞歸函數 } } }

相關文章
相關標籤/搜索