/// <summary> /// 获取文件夹下所有的文件夹列表 /// </summary> /// <param name="_original_path">遍历的文件夹</param> /// <param name="folderList">文件夹列表</param> /// <param name="haveFile">是否有文件</param> public static void getFolderList(string _original_path, ref List <string> folderList, int haveFile) { if (!string.IsNullOrEmpty(_original_path) && System.IO.Directory.Exists(_original_path)) { //folderList.Add(_original_path); DirectoryInfo folder = new DirectoryInfo(_original_path); //判断是否有文件 if (haveFile == 1) { if (folder.GetFiles().Length > 0) { folderList.Add(_original_path); } } else { folderList.Add(_original_path); } //开始遍历子文件夹 //遍历子文件夹 DirectoryInfo[] dirInfo = folder.GetDirectories(); string folderPath = ""; foreach (DirectoryInfo NextFolder in dirInfo) { folderPath = _original_path + "\\" + NextFolder.ToString(); getFolderList(folderPath, ref folderList, haveFile); } } }
public static List <string> FindFolders(string sSourcePath) { List <String> list = new List <string>(); //遍历文件夹 DirectoryInfo theFolder = new DirectoryInfo(sSourcePath); DirectoryInfo[] dirInfo = theFolder.GetDirectories(); foreach (DirectoryInfo NextFolder in dirInfo) { list.Add(NextFolder.ToString()); //FileInfo[] fileInfo = NextFolder.GetFiles("*.*", SearchOption.AllDirectories); //foreach (FileInfo NextFile in fileInfo) //遍历文件 //list.Add(NextFile.FullName); } return(list); }
public IPagedList <ArgumentItem> GetPagedArgumentData(int?processDID, int pageNo, int pageSize) { List <ArgumentItem> lst = new List <ArgumentItem>(); string path = "C:\\Users\\junxi\\Desktop\\bat"; DirectoryInfo theFolder = new DirectoryInfo(path); DirectoryInfo[] dirInfo = theFolder.GetDirectories(); foreach (DirectoryInfo NextFolder in dirInfo) { ArgumentItem info = new ArgumentItem(); info.Name = NextFolder.ToString(); lst.Add(info); } IQueryable <ArgumentItem> superset = lst.AsQueryable(); return(new PagedList <ArgumentItem>(superset, pageNo, pageSize)); }
public static string GetMyFolderInfo(string sSourcePath) { //遍历文件夹 Dictionary <String, List <String> > dict = new Dictionary <string, List <string> >(); DirectoryInfo theFolder = new DirectoryInfo(sSourcePath); //遍历子文件夹 DirectoryInfo[] dirInfo = theFolder.GetDirectories(); foreach (DirectoryInfo NextFolder in dirInfo) { FileInfo[] fileInfo = NextFolder.GetFiles("*.*", SearchOption.AllDirectories); //获取子文件夹及其子文件夹(有的话)文件信息 List <String> list = new List <string>(); foreach (FileInfo NextFile in fileInfo) //遍历文件 { list.Add(NextFile.Name); } dict.Add(NextFolder.ToString(), list); } string resInfo = ConvertToJson(dict); return(resInfo); }
/* * public List<Dictionary<string, string>> queryFilelists(string path, string ext) * { * if ((path[path.Length - 1] != '\\') & (path[path.Length - 1] != '/')) * path = path + "\\"; * * List<Dictionary<string, string>> fileList = new List<Dictionary<string, string>>(); * //ArrayList fileList = new ArrayList(); * * List<String> list = new List<string>(); * DirectoryInfo theFolder = new DirectoryInfo(path); * FileInfo[] thefileInfo = theFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly); * * * if (comboBox_FilePath.Text.Length > 3) * { * Dictionary<string, string> dict = new Dictionary<string, string>(); * dict["name"] = ".."; * * dict["size"] = ""; * dict["type"] = Folder.ToString(); * dict["lastModified"] = ""; * * fileList.Add(dict); * list.Add(".."); * } * * foreach (FileInfo NextFile in thefileInfo) * { * if (NextFile.Extension.Length < 1) * continue; * * if (ext.IndexOf(NextFile.Extension.ToLower()) < 0) * continue; * * if (NextFile.Length == 0) * continue; * * * Dictionary<string, string> dict = new Dictionary<string, string>(); * * if (checkBox1_SubFolder.Checked) * dict["name"] = get_relative_path(comboBox_FilePath.Text, NextFile.FullName); * else * dict["name"] = NextFile.Name; * * dict["size"] = NextFile.Length.ToString(); * dict["type"] = File.ToString(); * dict["lastModified"] = NextFile.LastAccessTime.ToString(); * * fileList.Add(dict); * list.Add(NextFile.FullName); * } * * DirectoryInfo[] dirInfo = theFolder.GetDirectories(); * foreach (DirectoryInfo NextFolder in dirInfo) * { * * list.Add(NextFolder.ToString()); * * Dictionary<string, string> dict1 = new Dictionary<string, string>(); * * dict1["name"] = NextFolder.Name; * dict1["size"] = ""; * dict1["type"] = Folder.ToString(); //0 folder, 1 file * dict1["lastModified"] = NextFolder.LastAccessTime.ToString(); * * fileList.Add(dict1); * * * if (checkBox1_SubFolder.Checked == false) * { * continue; * } * * * FileInfo[] fileInfo = NextFolder.GetFiles("*.*", SearchOption.AllDirectories); * foreach (FileInfo NextFile in fileInfo) * { * if (NextFile.Extension.Length < 1) * continue; * * if (ext.IndexOf(NextFile.Extension.ToLower()) < 0) * continue; * * if (NextFile.Length == 0) * continue; * * //skip backup dir's file * if (NextFile.FullName.IndexOf(BackupPath) == 0) * continue; * * if (NextFile.Name.IndexOf(".Designer.cs") >= 0) * continue; * * * Dictionary<string, string> dict = new Dictionary<string, string>(); * * if (checkBox1_SubFolder.Checked) * dict["name"] = get_relative_path(comboBox_FilePath.Text, NextFile.FullName); * else * dict["name"] = NextFile.Name; * * dict["size"] = NextFile.Length.ToString(); * dict["type"] = File.ToString(); //0 folder, 1 file * dict["lastModified"] = NextFile.LastAccessTime.ToString(); * * fileList.Add(dict); * list.Add(NextFile.FullName); * } * } * * return fileList; * } */ public List <Dictionary <string, string> > queryFilelists(string spath, string ext) { if ((spath[spath.Length - 1] != '\\') & (spath[spath.Length - 1] != '/')) { spath = spath + "\\"; } List <Dictionary <string, string> > fileList = new List <Dictionary <string, string> >(); //ArrayList fileList = new ArrayList(); List <String> list = new List <string>(); DirectoryInfo theFolder = new DirectoryInfo(spath); FileInfo[] thefileInfo = theFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly); if (spath.Length > 3) { Dictionary <string, string> dict = new Dictionary <string, string>(); dict["name"] = ".."; dict["size"] = ""; dict["type"] = SFileType.Folder_item.ToString(); dict["lastModified"] = ""; fileList.Add(dict); list.Add(".."); } addFilesItem(spath, ext, thefileInfo, ref fileList, ref list); DirectoryInfo[] dirInfo = { }; try { if (search_SubFolder) { dirInfo = theFolder.GetDirectories("*.*", SearchOption.AllDirectories); } else { dirInfo = theFolder.GetDirectories("*.*", SearchOption.TopDirectoryOnly); } } catch (Exception ex) { MessageBox.Show(ex.Message); } foreach (DirectoryInfo NextFolder in dirInfo) { //ignore .git //if (NextFolder.FullName.IndexOf(@"\.git\") >= 0) //{ // continue;//bypass .git //} if (isValidFolder(NextFolder) == false) { continue; } //FileInfo[] fileInfo = NextFolder.GetFiles("*.*", SearchOption.AllDirectories); FileInfo[] fileInfo = { }; try { fileInfo = NextFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly); } catch (Exception) { } if (fileInfo.Count() == 0) { continue;//bypass empty folder } List <Dictionary <string, string> > fileListSubDir = new List <Dictionary <string, string> >(); //foreach (FileInfo NextFile in fileInfo) //{ // if (NextFile.Extension.Length < 1) // continue; // if (ext.IndexOf(NextFile.Extension.ToLower()) < 0) // continue; // if (NextFile.Length == 0) // continue; // //skip backup dir's file // if (NextFile.FullName.IndexOf(App_Config.BackupPath) == 0) // continue; // if (NextFile.Name.IndexOf(".Designer.cs") >= 0) // continue; // Dictionary<string, string> dict = new Dictionary<string, string>(); // if (search_SubFolder) // dict["name"] = get_relative_path(spath, NextFile.FullName); // else // dict["name"] = NextFile.Name; // dict["size"] = NextFile.Length.ToString(); // dict["type"] = SFileType.File_item.ToString(); //0 folder, 1 file // dict["lastModified"] = NextFile.LastAccessTime.ToString(); // dict["FullName"] = NextFile.FullName; // fileListSubDir.Add(dict); //} addFilesItem(spath, ext, fileInfo, ref fileListSubDir, ref list); if (fileListSubDir.Count() > 0) { Dictionary <string, string> dict1 = new Dictionary <string, string>(); //dict1["name"] = NextFolder.Name; dict1["name"] = get_relative_path(spath, NextFolder.FullName); dict1["size"] = ""; dict1["type"] = SFileType.Folder_item.ToString(); //0 folder, 1 file dict1["lastModified"] = NextFolder.LastAccessTime.ToString(); fileList.Add(dict1); list.Add(NextFolder.ToString()); } if (search_SubFolder == false) { continue; } foreach (Dictionary <string, string> dict12 in fileListSubDir) { fileList.Add(dict12); list.Add(dict12["FullName"]); } } return(fileList); }