遍历多层文件夹下的所有文件并把文件名和路径导入到数据库
public static void FindFile(string dir) // C:\Documents and Settings\禹创\桌面\原始目录\
{
string delete_sql = @"DELETE FROM [Platform_Danone_BusinessData].[dbo].[T_Template_File]";
DataAccess(delete_sql);
//DeleteData();
DirectoryInfo Dir = new DirectoryInfo(dir);
string Temp_filename = string.Empty;
string FilecodeName = string.Empty;
try
{
foreach (DirectoryInfo d in Dir.GetDirectories())
{
FindFile(Dir + d.ToString() + "\\");
}
foreach (FileInfo f in Dir.GetFiles("*.*")) //这里"*.*"代表查找所有的文件类型,可以修改为"*.___"(___代表文件的后缀);
{
Temp_filename = f.Name.Substring(0, 2);
//过滤文件夹下的临时文件
if (Temp_filename != "~$")
{
string Filepath = ((System.IO.FileSystemInfo)(Dir)).FullName +@"\"+ f.Name;
//文件二进制
byte[] bytContent = wordConvertByte(Filepath);
//RecordID
string RecordID = GetRnd(16, true, true, true, false, "");
string Filename = f.Name;
string Filetype=f.Name.Substring(f.Name.IndexOf('.'));
string FileDate = System.DateTime.Now.ToString();
InsertGreenDoc(RecordID, Filename, Filetype, FileDate, bytContent, Filepath);
Temp_filename = string.Empty;
}
}
string update_sql = @" update [Platform_Danone].[dbo].[T_RESOURCE]
set [Res_Data1]=''
where [Res_Parent_Code] in
(select [Res_Code]
from [Platform_Danone].[dbo].[T_RESOURCE]
where [Res_Parent_Code]='00008000210000000000000000000000000000000000000000')";
DataAccess(update_sql);
}
catch (Exception e)
{
throw e;
}
}
public static byte[] wordConvertByte(string wordPath)
{
byte[] bytContent = null;
System.IO.FileStream fs = null;
System.IO.BinaryReader br = null;
try
{
fs = new FileStream(wordPath, System.IO.FileMode.Open);
br = new BinaryReader((Stream)fs);
bytContent = br.ReadBytes((Int32)fs.Length);
}
catch (Exception ex)
{
throw ex;
}
return bytContent;
}

浙公网安备 33010602011771号