如何获取指定目录包含的文件和子目录:
采用递归的方式遍历,文件夹和子目录中的所有文件:
public void FileList(FileSystemInfo f)//FileList为自己创建的方法
{
if (!f.Exists)//先判断f所指的文件或文件夹是否存在
{
return;
}
DirectoryInfo di = f as DirectoryInfo;
if (di == null)
{
return;
}
FileSystemInfo[] fs = di.GetFileSystemInfos();//获取文件夹中所有文件和文件夹
//下而对单个FileSystemInfo进行判断,如果是文件夹则进行递归操作
foreach (FileSystemInfo f2 in fs)
{
FileInfo file = f2 as FileInfo;
if (file != null)
{
Response.Write(file.Directory + @"\" + file.Name + "<br />");
}
else
{
FileList(f2);
}
}
}
递归方法调用【将数据库中不存在的图片及其物理路径删除掉】:
protected void Page_Load(object sender, EventArgs e)
{
//指定路径的目录
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Image1/images_news/sy/Upload/"));
FileList(dir);
string str = img_url.Value.Trim();
string[] images = str.Substring(0, str.Length - 1).Split(',');
sqlConnection1.ConnectionString = conn;
sqlCommand1.Connection = sqlConnection1;
sqlConnection1.Open();
//创建一个临时表,存储指定文件夹下的所有文件
sqlCommand1.CommandText = "create table ##temp(id int,file_path varchar(1000),flag int)";
sqlCommand1.ExecuteNonQuery();
for (int i = 0; i < images.Length; i++)
{
sqlCommand1.CommandText = "insert into ##temp(id,file_path,flag) values (" + (i + 1) + ",'" + images[i] + "',1)";
sqlCommand1.ExecuteNonQuery();
}
//读取数据库中存在图片的数据
sqlCommand1.CommandText = @"select image_thumb from news_tb where image_thumb is not null and image_thumb<>'' order by id desc";
sqlDataReader1 = sqlCommand1.ExecuteReader();
while (sqlDataReader1.Read())
{
sqlConnection2.ConnectionString = conn;
sqlCommand2.Connection = sqlConnection2;
sqlConnection2.Open();
string img = sqlDataReader1["image_thumb"].ToString().Replace("/", @"\").Trim();
sqlCommand2.CommandText = "update ##temp set flag='0' where file_path like '%" + img + "%'";
sqlCommand2.ExecuteNonQuery();
sqlConnection2.Close();
sqlConnection2.Dispose();
}
sqlDataReader1.Close();
//将临时表中标记为1的对应的图片及其所在物理路径删除。。。
sqlCommand1.CommandText = "select * from ##temp where flag='1'";
sqlDataReader1 = sqlCommand1.ExecuteReader();
while (sqlDataReader1.Read())
{
string f = sqlDataReader1["file_path"].ToString().Trim();
//删除文件
if (File.Exists(f))
{
File.Delete(f);
}
//删除该图像所在的文件夹
string floder = Path.GetDirectoryName(f);
while (Directory.Exists(floder) && Directory.GetFiles(floder).Length == 0 && Directory.GetDirectories(floder).Length == 0)
{
Directory.Delete(floder);
}
}
sqlDataReader1.Close();
sqlConnection1.Close();
sqlConnection1.Dispose();
采用递归的方式遍历,文件夹和子目录中的所有文件:
public void FileList(FileSystemInfo f)//FileList为自己创建的方法
{
if (!f.Exists)//先判断f所指的文件或文件夹是否存在
{
return;
}
DirectoryInfo di = f as DirectoryInfo;
if (di == null)
{
return;
}
FileSystemInfo[] fs = di.GetFileSystemInfos();//获取文件夹中所有文件和文件夹
//下而对单个FileSystemInfo进行判断,如果是文件夹则进行递归操作
foreach (FileSystemInfo f2 in fs)
{
FileInfo file = f2 as FileInfo;
if (file != null)
{
Response.Write(file.Directory + @"\" + file.Name + "<br />");
}
else
{
FileList(f2);
}
}
}
递归方法调用【将数据库中不存在的图片及其物理路径删除掉】:
protected void Page_Load(object sender, EventArgs e)
{
//指定路径的目录
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Image1/images_news/sy/Upload/"));
FileList(dir);
string str = img_url.Value.Trim();
string[] images = str.Substring(0, str.Length - 1).Split(',');
sqlConnection1.ConnectionString = conn;
sqlCommand1.Connection = sqlConnection1;
sqlConnection1.Open();
//创建一个临时表,存储指定文件夹下的所有文件
sqlCommand1.CommandText = "create table ##temp(id int,file_path varchar(1000),flag int)";
sqlCommand1.ExecuteNonQuery();
for (int i = 0; i < images.Length; i++)
{
sqlCommand1.CommandText = "insert into ##temp(id,file_path,flag) values (" + (i + 1) + ",'" + images[i] + "',1)";
sqlCommand1.ExecuteNonQuery();
}
//读取数据库中存在图片的数据
sqlCommand1.CommandText = @"select image_thumb from news_tb where image_thumb is not null and image_thumb<>'' order by id desc";
sqlDataReader1 = sqlCommand1.ExecuteReader();
while (sqlDataReader1.Read())
{
sqlConnection2.ConnectionString = conn;
sqlCommand2.Connection = sqlConnection2;
sqlConnection2.Open();
string img = sqlDataReader1["image_thumb"].ToString().Replace("/", @"\").Trim();
sqlCommand2.CommandText = "update ##temp set flag='0' where file_path like '%" + img + "%'";
sqlCommand2.ExecuteNonQuery();
sqlConnection2.Close();
sqlConnection2.Dispose();
}
sqlDataReader1.Close();
//将临时表中标记为1的对应的图片及其所在物理路径删除。。。
sqlCommand1.CommandText = "select * from ##temp where flag='1'";
sqlDataReader1 = sqlCommand1.ExecuteReader();
while (sqlDataReader1.Read())
{
string f = sqlDataReader1["file_path"].ToString().Trim();
//删除文件
if (File.Exists(f))
{
File.Delete(f);
}
//删除该图像所在的文件夹
string floder = Path.GetDirectoryName(f);
while (Directory.Exists(floder) && Directory.GetFiles(floder).Length == 0 && Directory.GetDirectories(floder).Length == 0)
{
Directory.Delete(floder);
}
}
sqlDataReader1.Close();
sqlConnection1.Close();
sqlConnection1.Dispose();
}
备注:##全局临时表,#临时表。临时表只存在于一个数据库链接中,当数据库链接关闭的时候,临时表也会被删掉;全局临时表则没有数据库链接的局限性,多个数据库链接也可以调用。