使用一个按钮就可查找出文件,是不是很方便,这样就快速帮我们找到它的具体位置
protected void Button1_Click(object sender, EventArgs e) { string path = ""; string physicsPath = Server.MapPath(Request.ApplicationPath); //将当前虚拟根路径转为实际物理路径 string toFindDirectoryName = "ss"; //要查找的文件夹名 FindDirectory(physicsPath + "\\", toFindDirectoryName, out path);//用递归的方式去查找文件夹 if (!string.IsNullOrEmpty(path)) //如果存在,返回该文件夹所在的物理路径 { //将该物理路径转为虚拟路径 Response.Write(GetVirtualPath(path, Request.ApplicationPath)); } else { //没有找到路径,创建新文件夹 Directory.CreateDirectory(physicsPath + "\\" + toFindDirectoryName); } } ////// 将物理路径转为虚拟路径 /// /// 物理路径 /// 虚拟根路径 ///private string GetVirtualPath(string physicsPath, string virtualRootPath) { int index = physicsPath.IndexOf(virtualRootPath.Substring(1)); return "/" + physicsPath.Substring(index).Replace("\\", "/"); } /// /// 在指定目录下递归查找子文件夹 /// /// 根文件夹路径 /// 要查找的文件夹名 private void FindDirectory(string bootPath, string directoryName, out string filePath) { //在指定目录下递归查找子文件夹 DirectoryInfo dir = new DirectoryInfo(bootPath); filePath = ""; try { foreach (DirectoryInfo d in dir.GetDirectories()) //查找子文件夹 { if (d.Name == directoryName) //找到,返回文件夹路径 { filePath = d.FullName; break; } FindDirectory(bootPath + d.Name + "\\", directoryName, out filePath); //否则继续查找 } } catch (Exception e) { Response.Write(e.Message); } }