asp.net get all subdirectories and files under the specified folder (tree) _ Practical Tips

Source: Internet
Author: User
#region get all subdirectories and files under the specified folder (tree)
/****************************************
* Function Name: Getfoldall (String Path)
* Function Description: Get all subdirectories and files under the specified folder (tree form)
* Parameter: Path: detailed path
* Call Columns:
* String strdirlist = Server.MapPath ("templates");
* this. Literal1.text = EC. Fileobj.getfoldall (strdirlist);
*****************************************/
<summary>
Get all subdirectories and files under the specified folder
</summary>
<param name= "path" > Detailed path </param>
public static string Getfoldall (String Path)
{

String str = "";
DirectoryInfo thisone = new DirectoryInfo (Path);
str = listtreeshow (thisone, 0, str);
return str;

}

<summary>
Gets all subdirectories and file functions under the specified folder
</summary>
<param name= "Thedir" > specified directory </param>
<param name= "Nlevel" > Default starting Value, when called, typically 0</param>
<param name= "Rn" > the incoming value for the iteration, generally null </param>
<returns></returns>
public static string Listtreeshow (DirectoryInfo thedir, int nlevel, string Rn)//recursive directory file
{
directoryinfo[] subdirectories = thedir.getdirectories ()//Get Directory
foreach (DirectoryInfo dirinfo in subdirectories)
{

if (nlevel = 0)
{
Rn + "├";
}
Else
{
String _s = "";
for (int i = 1; I <= nlevel; i++)
{
_s + + "│";
}
Rn + _s + "├";
}
Rn + + "<b>" + dirinfo. Name.tostring () + "</b><br/>";
fileinfo[] FileInfo = Dirinfo.   GetFiles (); Files under the directory
foreach (FileInfo finfo in FileInfo)
{
if (nlevel = 0)
{
Rn + "│";
}
Else
{
String _f = "";
for (int i = 1; I <= nlevel; i++)
{
_f + + "│";
}
Rn + + _f + "│";
}
Rn + fInfo.Name.ToString () + "<br/>";
}
RN = Listtreeshow (dirinfo, Nlevel + 1, RN);


}
return Rn;
}



/****************************************
* Function Name: Getfoldall (String Path)
* Function Description: Get all subdirectories and files under the specified folder (dropdown box shape)
* Parameter: Path: detailed path
* Call Columns:
* String strdirlist = Server.MapPath ("templates");
* this. Literal2.text = EC. Fileobj.getfoldall (Strdirlist, "TPL", "");
*****************************************/
<summary>
Gets all subdirectories and files under the specified folder (dropdown box shape)
</summary>
<param name= "path" > Detailed path </param>
<param name= "Dropname" > Drop-down list name </param>
<param name= "Tplpath" > Default selection Template name </param>
public static string Getfoldall (String path,string dropname,string Tplpath)
{
String strdrop = "<select name=\" "+ dropname +" \ "id=\" "+ dropname +" \ "><option value=\" \ ">--Please select a detailed template--< /option> ";
String str = "";
DirectoryInfo thisone = new DirectoryInfo (Path);
str = listtreeshow (thisone, 0, Str,tplpath);
return strdrop+str+ "</select>";

}

<summary>
Gets all subdirectories and file functions under the specified folder
</summary>
<param name= "Thedir" > specified directory </param>
<param name= "Nlevel" > Default starting Value, when called, typically 0</param>
<param name= "Rn" > the incoming value for the iteration, generally null </param>
<param name= "Tplpath" > Default selection Template name </param>
<returns></returns>
public static string Listtreeshow (DirectoryInfo thedir, int nlevel, string rn,string tplpath)//recursive directory file
{
directoryinfo[] subdirectories = thedir.getdirectories ()//Get Directory

foreach (DirectoryInfo dirinfo in subdirectories)
{

Rn = "<option value=\" + dirinfo. Name.tostring () + "\";
if (tplpath.tolower () = = Dirinfo. Name.tostring (). ToLower ())
{
Rn + "selected";
}
Rn + ">";

if (nlevel = 0)
{
Rn + ";"
}
Else
{
String _s = "";
for (int i = 1; I <= nlevel; i++)
{
_s + + "│";
}
Rn + + _s + ";"
}
Rn + = "" + dirinfo. Name.tostring () + "</option>";


fileinfo[] FileInfo = Dirinfo.   GetFiles (); Files under the directory
foreach (FileInfo finfo in FileInfo)
{
Rn = "<option value=\" + dirinfo. Name.tostring () + "/" +finfo.name.tostring () + "\";
if (tplpath.tolower () = = FInfo.Name.ToString (). ToLower ())
{
Rn + "selected";
}
Rn + ">";

if (nlevel = 0)
{
Rn + "│";
}
Else
{
String _f = "";
for (int i = 1; I <= nlevel; i++)
{
_f + + "│";
}
Rn + + _f + "│";
}
Rn + fInfo.Name.ToString () + "</option>";
}
RN = Listtreeshow (dirinfo, Nlevel + 1, RN, Tplpath);


}
return Rn;
}
#endregion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.