#region 获取目录

        /// <summary>
/// 获取指定文件夹下所有子目录及文件
/// </summary>
/// <param name="Path">详细路径</param>
public static string GetFoldAll(string Path)
{ string str = "";
DirectoryInfo thisOne = new DirectoryInfo(Path);
str = ListTreeShow(thisOne, , 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 == )
{
Rn += "├";
}
else
{
string _s = "";
for (int i = ; i <= nLevel; i++)
{
_s += "│ ";
}
Rn += _s + "├";
}
Rn += "<b>" + dirinfo.Name.ToString() + "</b><br />";
FileInfo[] fileInfo = dirinfo.GetFiles(); //目录下的文件
foreach (FileInfo fInfo in fileInfo)
{
if (nLevel == )
{
Rn += "│ ├";
}
else
{
string _f = "";
for (int i = ; i <= nLevel; i++)
{
_f += "│ ";
}
Rn += _f + "│ ├";
}
Rn += fInfo.Name.ToString() + " <br />";
}
Rn = ListTreeShow(dirinfo, nLevel + , 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, , 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 == )
{
Rn += "┣";
}
else
{
string _s = "";
for (int i = ; 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 == )
{
Rn += "│ ├";
}
else
{
string _f = "";
for (int i = ; i <= nLevel; i++)
{
_f += "│ ";
}
Rn += _f + "│ ├";
}
Rn += fInfo.Name.ToString() + "</option>";
}
Rn = ListTreeShow(dirinfo, nLevel + , Rn, tplPath); }
return Rn;
}
#endregion

C# 循环获取目录的更多相关文章

  1. php获取目录中的所有文件名

    <?php /** * [php获取目录中的所有文件名] */ //1.先打开要操作的目录,并用一个变量指向它 //打开当前目录下的目录pic下的子目录common. $handler = op ...

  2. C# 获取目录下文件

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. PHP获取目录中的全部内容RecursiveDirectoryIterator

    这次我们来介绍一个SPL库中的目录迭代器,它的作用其实非常简单,从名字就可以看出来,就是获取指定目录下的所有内容.之前我们要遍历目录获取目录及目录下的所有文件一般是需要进行递归遍历的,自己写这个代码说 ...

  4. Linux C 程序 获取目录信息(16)

    4.获取当前目录getcwd 会将当前工作目录绝对路径复制到参数buf所指的内存空间5.设置工作目录chdir6.获取目录信息opendir打开一个目录readdir读取目录中的内容  读取目录项信息 ...

  5. (转载)php循环检测目录是否存在并创建(循环创建目录)

    (转载)http://www.jb51.net/article/25917.htm php循环检测目录是否存在并创建,需要的朋友可以参考下. 循环创建目录方法 这个会生成image.gif目录 代码如 ...

  6. 取CPU序列号,获取网卡,取硬盘系列号,获取目录下的文件,强制删除目录

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  7. 老李推荐:第5章7节《MonkeyRunner源码剖析》Monkey原理分析-启动运行: 循环获取并执行事件 - runMonkeyCycles

    老李推荐:第5章7节<MonkeyRunner源码剖析>Monkey原理分析-启动运行: 循环获取并执行事件 - runMonkeyCycles   poptest是国内唯一一家培养测试开 ...

  8. jquery 循环获取checkBox的值,以及对复选框选中,取消,操作按钮

    法一:jquery 循环获取选中checkBox框的值 function checkeds() { $("input:checkbox").each(function(index) ...

  9. python生成器 获取 目录下文件

    # os.walk()和os.list 都是得到所有文件的列表, 如果目录下文件特别多, 上亿了, 我们就需要生成器的方式获取 # 要求目录下面没有目录, 会递归到子目录下面找文件, (如果有子目录可 ...

随机推荐

  1. WPF如何在同一个区域依次叠加显示多张图片呢?

    正如标题的问题,有时需要在已显示的图片的右上角(或其他区域)显示小图标,譬如下图的患者头像右上角显示病情图标:(这里不采用事先用PS编排成一个图片文件的方式,因为此方式普适性不好) 解决方案:绘制该复 ...

  2. delphi7在win7系统如何安装spcomm控件

    1.先准备好串口控件SPCOMM,例如把它放在F盘的工具安装文件夹下,等一下加载时需要用到. 2.打开delphi7软件. 3.按下上面的Component>Install  Component ...

  3. Cake slicing

    题意: n*m的方格中有k个点,现在要把方格分开使得每个点在一个部分,每分一次花费边长的费用,求完成花的最小费用 分析: dp[sx][sy][ex][ey]表示分割起点(sx,sy)终点(ex,ey ...

  4. HDU 4630-No Pain No Game(线段树+离线处理)

    题意: 给你n个数的序列a,q个询问,每个询问给l,r,求在下标i在[l,r]的区间任意两个数的最大公约数中的最大值 分析: 有了hdu3333经验,我们从左向右扫序列,如果当前数的约数在前面出现过, ...

  5. XCode修改工程名注意

    以下文字转载过来,在使用的过程中遇到几个问题 1.需要在 Build phases 里面,检查下 Link Binary With Libraries 以及Compline Sources 2.Bul ...

  6. ASP.NET MVC3 系列教程 - Razor视图引擎基础语法

    http://www.cnblogs.com/highend/archive/2011/04/09/aspnet_mvc3_razor_engine.html 4. 关于所有带"_" ...

  7. VSim [a Racing-simulator by Vell001]

    VSim [a racing-simulator by vell001] This is my first project about Racing. I am a Chinese with bad ...

  8. SOP、DIP、PLCC、TQFP、PQFP、TSOP、BGA封装解释

    1. SOP封装SOP是英文Small Outline Package的缩写,即小外形封装.SOP封装技术由1968-1969年菲利浦公司开发成功,以后逐渐派生出SOJ(J型引脚小外形封装).TSOP ...

  9. HDU 1856 More is better(并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others) ...

  10. 怎样提交FIREDAC数据集的DELTA到中间件然后保存进数据库

    你可以在客户端序列FireDAC数据集的DELTA , 将序列后的STREAM发送给中间件, 中间件的TFDQuery或TFDMemTable调用LOADFROMSTREAM()方法加载流, 然后调用 ...