/// <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,".txt"); for (int i = 0; i < file.Length; i++) { //FileInfo fi = new FileInfo(file[i]); //if (fi.Extension.ToLower() == "txt") //{ result.Add(file[i]); //} } }
private void forFileLength(DirectoryInfo directory) { DirectoryInfo[] directorys = directory.GetDirectories(); FileInfo[] files; foreach (DirectoryInfo di in directorys) { forFileLength(di); } files = directory.GetFiles(); foreach(FileInfo file in files) { string temp = file.DirectoryName.ToString(); // 当前路径件 string name = file.Name; // name 文件名 } } public void FoundFile() { DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/公司/"+gongsiname)); DataTable dt = new DataTable(); dt.Columns.Add("imgurl", typeof(String)); foreach (FileInfo fi in di.GetFiles()) { DataRow dr = dt.NewRow(); //dr[0] = Server.MapPath("公司/") + fi.Name; dr[0] = "~/公司/"+gongsiname+"/"+ fi.Name; dt.Rows.Add(dr); } this.DataList1.DataSource = dt; this.DataList1.DataBind(); //this.GridView1.DataSource = dt; //this.GridView1.DataBind(); } 取出文件名,并绑定到DataList上面 protected void Page_Load(object sender, EventArgs e) { string gongsiname=Request["gongsiname"].ToString (); DirectoryInfo imagesfile = new DirectoryInfo("G://公司//"+gongsiname); DataList1.DataSource = imagesfile.GetFiles("*.jpg"); DataList1.DataBind(); }