#region 獲取指定文件夾下所有子目錄及文件(樹形)
/****************************************
* 函數名稱:GetFoldAll(string Path)
* 功能說明:獲取指定文件夾下所有子目錄及文件(樹形)
* 參 數:Path:詳細路徑
* 調用示列:
* string strDirlist = Server.MapPath("templates");
* this.Literal1.Text = EC.FileObj.GetFoldAll(strDirlist);
*****************************************/
/// <summary>
/// 獲取指定文件夾下所有子目錄及文件
/// </summary>
/// <param name="Path">詳細路徑</param>
public static string GetFoldAll(string Path)
{
string str = "";
DirectoryInfo thisOne = new DirectoryInfo(Path);
str = ListTreeShow(thisOne, 0, str);
return str;
}
/// <summary>
/// 獲取指定文件夾下所有子目錄及文件函數
/// </summary>
/// <param name="theDir">指定目錄</param>
/// <param name="nLevel">默認起始值,調用時,一般為0</param>
/// <param name="Rn">用於迭加的傳入值,一般為空</param>
/// <returns></returns>
public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn)//遞歸目錄 文件
{
DirectoryInfo[] subDirectories = theDir.GetDirectories();//獲得目錄
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(); //目錄下的文件
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;
}
/****************************************
* 函數名稱:GetFoldAll(string Path)
* 功能說明:獲取指定文件夾下所有子目錄及文件(下拉框形)
* 參 數:Path:詳細路徑
* 調用示列:
* string strDirlist = Server.MapPath("templates");
* this.Literal2.Text = EC.FileObj.GetFoldAll(strDirlist,"tpl","");
*****************************************/
/// <summary>
/// 獲取指定文件夾下所有子目錄及文件(下拉框形)
/// </summary>
/// <param name="Path">詳細路徑</param>
///<param name="DropName">下拉列表名稱</param>
///<param name="tplPath">默認選擇模板名稱</param>
public static string GetFoldAll(string Path,string DropName,string tplPath)
{
string strDrop = "<select name=\"" + DropName + "\" id=\"" + DropName + "\"><option value=\"\">--請選擇詳細模板--</option>";
string str = "";
DirectoryInfo thisOne = new DirectoryInfo(Path);
str = ListTreeShow(thisOne, 0, str,tplPath);
return strDrop+str+"</select>";
}
/// <summary>
/// 獲取指定文件夾下所有子目錄及文件函數
/// </summary>
/// <param name="theDir">指定目錄</param>
/// <param name="nLevel">默認起始值,調用時,一般為0</param>
/// <param name="Rn">用於迭加的傳入值,一般為空</param>
/// <param name="tplPath">默認選擇模板名稱</param>
/// <returns></returns>
public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn,string tplPath)//遞歸目錄 文件
{
DirectoryInfo[] subDirectories = theDir.GetDirectories();//獲得目錄
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(); //目錄下的文件
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