/// <summary> /// 从指定目录下查找所有包含指定字符的所有文件,生成数据列表返回。 /// 数据列表包括字段(name(文件名),type(2),size(文件大小,单位:字节),content_type(文件扩展名),createTime(创建时间),lastWriteTime(最后修改时间)) /// </summary> /// <param name="dir">要搜索的目录</param> /// <param name="searchPattern">搜索模式如(*.*)</param> /// <param name="searchKeyword">要搜索的字符</param> /// <returns>包含关键词的文件列表,包括字段(name(文件名),type(2),size(文件大小,单位:字节),content_type(文件扩展名),createTime(创建时间),lastWriteTime(最后修改时间))</returns> public static List <DirDetail> SearchFileContent(string dir, string searchPattern, string searchKeyword) { dir = MapPath(dir); List <DirDetail> table = new List <DirDetail>(); DirectoryInfo info = new DirectoryInfo(dir); foreach (FileInfo info2 in info.GetFiles(searchPattern, SearchOption.AllDirectories)) { DirDetail ddInfo = new DirDetail(); StreamReader reader = info2.OpenText(); string str = reader.ReadToEnd(); reader.Dispose(); if (str.Contains(searchKeyword)) { ddInfo.Name = info2.FullName.Remove(0, info.FullName.Length); ddInfo.Rname = info2.FullName; ddInfo.Path = info2.DirectoryName + DirectorySeparatorChar; ddInfo.Type = 2; ddInfo.Size = info2.Length; ddInfo.Content_type = info2.Extension.Replace(".", string.Empty); ddInfo.Creatime = info2.CreationTime; ddInfo.LastWriteTime = info2.LastWriteTime; table.Add(ddInfo); } } return(table); }
/// <summary> /// 得到指定目录下的所有目录和子目录或文件的详细信息 /// </summary> /// <param name="d"></param> /// <param name="method"></param> /// <returns></returns> private static List <DirDetail> GetDirectoryAllInfo(DirectoryInfo d, FileMethod method) { List <DirDetail> parent = new List <DirDetail>(); foreach (DirectoryInfo info in d.GetDirectories()) { if (method == FileMethod.File) { parent = CopyDT(parent, GetDirectoryAllInfo(info, method)); } else { DirDetail ddInfo = new DirDetail(); ddInfo.Name = info.Name; ddInfo.Rname = info.FullName; ddInfo.Content_type = string.Empty; ddInfo.Type = 1; ddInfo.Path = info.FullName.Replace(info.Name, string.Empty); ddInfo.Creatime = info.CreationTime; ddInfo.LastWriteTime = info.LastWriteTime; ddInfo.Size = 0; parent.Add(ddInfo); parent = CopyDT(parent, GetDirectoryAllInfo(info, method)); } } if (method != FileMethod.Folder) { foreach (FileInfo info2 in d.GetFiles()) { DirDetail ddInfo = new DirDetail(); ddInfo.Name = info2.Name; ddInfo.Rname = info2.FullName; ddInfo.Content_type = info2.Extension.Replace(".", string.Empty); ddInfo.Type = 2; ddInfo.Path = info2.DirectoryName + DirectorySeparatorChar; ddInfo.Creatime = info2.CreationTime; ddInfo.LastWriteTime = info2.LastWriteTime; ddInfo.Size = info2.Length; parent.Add(ddInfo); } } return(parent); }
/// <summary> /// 将指定目录路径下的所有目录不包括子目录或文件生成一张数据表 /// name(文件名) type(文件类型:1:文件夹;2:文件;) size(文件大小) content_type(文件扩展名) creatime(创建时间) lastWriteTime(修改时间) /// </summary> /// <param name="dir">要生成目录列表的文件夹路径</param> /// <param name="method">文件类型:文件夹或者文件</param> /// <returns></returns> public static List <DirDetail> GetDirectoryInfos(string dir, FileMethod method) { dir = MapPath(dir); List <DirDetail> table = new List <DirDetail>(); if (method != FileMethod.File) { for (int i = 0; i < Directory.GetDirectories(dir).Length; i++) { DirDetail ddInfo = new DirDetail(); DirectoryInfo d = new DirectoryInfo(Directory.GetDirectories(dir)[i]); long[] numArray = DirInfo(d); ddInfo.Name = d.Name; ddInfo.Rname = d.FullName; ddInfo.Path = d.FullName.Replace(d.Name, string.Empty); ddInfo.Type = 1; ddInfo.Size = numArray[0]; ddInfo.Content_type = string.Empty; ddInfo.Creatime = d.CreationTime; ddInfo.LastWriteTime = d.LastWriteTime; table.Add(ddInfo); } } if (method != FileMethod.Folder) { for (int j = 0; j < Directory.GetFiles(dir).Length; j++) { DirDetail ddInfo = new DirDetail(); FileInfo info2 = new FileInfo(Directory.GetFiles(dir)[j]); ddInfo.Name = info2.Name; ddInfo.Rname = info2.FullName; ddInfo.Path = info2.DirectoryName + @"\"; ddInfo.Type = 2; ddInfo.Size = info2.Length; ddInfo.Content_type = info2.Extension.Replace(".", string.Empty); ddInfo.Creatime = info2.CreationTime; ddInfo.LastWriteTime = info2.LastWriteTime; table.Add(ddInfo); } } return(table); }
/// <summary> /// 从指定目录下查找指定条件的模板文件(*.html,*.htm),生成数据列表 /// 数据列表包括字段(name(文件名),type(2),size(文件大小,单位:字节),content_type(文件扩展名),createTime(创建时间),lastWriteTime(最后修改时间)) /// </summary> /// <param name="dir">指定目录</param> /// <param name="searchPattern">搜索词:如"商品"</param> /// <returns>符合条件的模板文件列表,包括字段(name,type,size,content_type,createTime,lastWriteTime)</returns> public static List <DirDetail> SearchTemplateFiles(string dir, string searchPattern) { dir = MapPath(dir); List <DirDetail> table = new List <DirDetail>(); DirectoryInfo info = new DirectoryInfo(dir); string str = searchPattern; string str2 = searchPattern.ToLower(); int length = searchPattern.Length; if (length < 4) { str = "*" + str + "*.html"; } else if ((str2.Substring(length - 4, 4) != ".html") || (str2.Substring(length - 3, 3) != ".htm")) { str = "*" + str + "*.html"; } try { foreach (FileInfo info2 in info.GetFiles(str, SearchOption.AllDirectories)) { DirDetail ddInfo = new DirDetail(); ddInfo.Name = info2.FullName.Remove(0, info.FullName.Length).Replace("/", "\""); ddInfo.Rname = info2.FullName; ddInfo.Path = info2.DirectoryName + DirectorySeparatorChar; ddInfo.Type = 2; ddInfo.Size = info2.Length; ddInfo.Content_type = info2.Extension.Replace(".", string.Empty); ddInfo.Creatime = info2.CreationTime; ddInfo.LastWriteTime = info2.LastWriteTime; table.Add(ddInfo); } } catch (ArgumentException) { } return(table); }