上代码
using System.Collections.Specialized;
string strfilenames = "";
StringCollection sc01 = GetAllFiles(@"E:\Pro\MyProjectJianjie\MyProject\bd");
for (int i = 0; i < sc01.Count; i++)
{
//Console.WriteLine($"当前索引:{i},元素【{sc01[i]}】");
strfilenames += sc01[i] + "-";
string strout;
strout = "";
if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(urlconvertor(sc01[i]))))
{
}
else
{
StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(urlconvertor(sc01[i])), System.Text.Encoding.Default);
String input = sr.ReadToEnd();
sr.Close();
strout = input;
if (strout.IndexOf("")>=0)
{
//string path = System.Web.HttpContext.Current.Server.MapPath(urlconvertor(sc01[i]));
FileInfo fi = new FileInfo(sc01[i]);
if (fi.Exists)
{
fi.MoveTo(Server.MapPath(urlconvertor(sc01[i])+"123456"));
//Response.Write("<script>alert(\"文件改名成功\");</script>");
}
}
string a02 = "";
}
}
string a01 = "";
/// <summary>
/// 遍历 rootdir目录下的所有文件
/// </summary>
/// <param name="rootdir">目录名称</param>
/// <returns>该目录下的所有文件</returns>
public StringCollection GetAllFiles(string rootdir)
{
StringCollection result = new StringCollection();
GetAllFiles(rootdir, result);
return result;
}
/// <summary>
/// 作为遍历文件函数的子函数
/// </summary>
/// <param name="parentDir">目录名称</param>
/// <param name="result">该目录下的所有文件</param>
public void GetAllFiles(string parentDir, StringCollection result)
{
//获取目录parentDir下的所有的子文件夹
string[] dir = Directory.GetDirectories(parentDir);
for (int i = 0; i < dir.Length; i++)
GetAllFiles(dir[i], result);
//获取目录parentDir下的所有的文件,并过滤得到所有的文本文件
string[] file = Directory.GetFiles(parentDir, "*");
for (int i = 0; i < file.Length; i++)
{
//FileInfo fi = new FileInfo(file[i]);
//if (fi.Extension.ToLower() == "txt")
//{
result.Add(file[i]);
//}
}
}
private string urlconvertor(string imagesurl1)
{
string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
string imagesurl2 = imagesurl1.Replace(tmpRootDir, ""); //转换成相对路径
imagesurl2 = imagesurl2.Replace(@"\", @"/");
//imagesurl2 = imagesurl2.Replace(@"Aspx_Uc/", @"");
return imagesurl2;
}