遍历虚拟网站下所有目录
2010-03-26 09:03
651 查看
后台代码 protected string rtnIdObj, rtnIdPro;
protected string rtnNameObj, rtnNamePro;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TreeNode node = new TreeNode(this.RepalcePrevDir(Request.ApplicationPath), Request.ApplicationPath);
node.SelectAction = TreeNodeSelectAction.Expand;
tvLocalDirFile.Nodes.Add(node);
// 这里要移除根路径不是 / 的那些字符
ListAllPath(Request.ApplicationPath.Remove(1), tvLocalDirFile.Nodes[0]);
// 初始化传入的值
SplitIdAndValue();
}
}
/// <summary>
/// 分隔传到页面的值的 id 和 value 值,方便传回调用页面
/// </summary>
private void SplitIdAndValue()
{
rtnNamePro = rtnIdPro = "value"; // 都只能是 value 属性
string[] rtnId;
string[] rtnName;
if (!string.IsNullOrEmpty(Request["rtnText"]))
{
rtnId = Request["rtnText"].ToString().Split('.'); // 接收返回值的对象ID和属性
if (rtnId.Length == 2) // 将objectname.value 格式的传入参数,分解为对象名和属性两个字符串
{
rtnIdObj = rtnId[0];
rtnIdPro = rtnId[1];
}
}
if (!string.IsNullOrEmpty(Request["rtnValue"]))
{
rtnName = Request["rtnValue"].ToString().Split('.');
if (rtnName.Length == 2)
{
rtnNameObj = rtnName[0];
rtnNamePro = rtnName[1];
}
}
}
/// <summary>
/// 转换成相对路径
/// </summary>
/// <param name="imagesurl1">虚拟目录</param>
/// <returns></returns>
private string urlconvertor(string imagesurl1)
{
string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());// 获取程序根目录
string imagesurl2 = imagesurl1.Replace(tmpRootDir, ""); // 转换成相对路径
imagesurl2 = imagesurl2.Replace(@"\", @"/");
//imagesurl2 = imagesurl2.Replace(@"Aspx_Uc/", @"");
return imagesurl2;
}
// 转换成绝对路径
private string urlconvertorlocal(string imagesurl1)
{
string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());// 获取程序根目录
string imagesurl2 = tmpRootDir + imagesurl1.Replace(@"/", @"\"); // 转换成绝对路径
return imagesurl2;
}
/// <summary>
/// 列出网站根目录
/// </summary>
/// <param name="dirPath"></param>
/// <param name="passNode"></param>
private void ListAllPath(string dirPath, TreeNode passNode)
{
string[] dirs;
string[] files;
//Response.Write(VirtualPathUtility.GetDirectory(Request.ApplicationPath));
dirs = Directory.GetDirectories(this.urlconvertorlocal(dirPath));
files = Directory.GetFiles(this.urlconvertorlocal(dirPath));
for (int i = 0; i < dirs.Length; i++)
{
TreeNode node = new TreeNode(this.RepalcePrevDir(this.urlconvertor(dirs[i])), dirs[i]);
//node.NavigateUrl = "#";
node.SelectAction = TreeNodeSelectAction.Expand;
//node.SelectAction= TreeNodeSelectAction.
passNode.ChildNodes.Add(node); // 已经添加了节点
ListAllPath(this.urlconvertor(dirs[i]), passNode.ChildNodes[passNode.ChildNodes.IndexOf(node)]); // 索引到那个节点
}
for (int i = 0; i < files.Length; i++)
{
TreeNode tNode = new TreeNode(this.RepalcePrevDir(this.urlconvertor(files[i])), files[i]);
tNode.NavigateUrl = "javascript:SetTextValue('" + this.urlconvertor(files[i]) + "', '" + files[i].Replace("\\", "\\\\") + "')";
passNode.ChildNodes.Add(tNode);
}
}
/// <summary>
/// 去除掉路径前的所有父路径
/// </summary>
/// <param name="dir"></param>
/// <returns></returns>
private string RepalcePrevDir(string dir)
{
return dir.Substring(dir.LastIndexOf("/") + 1);
}
protected string rtnNameObj, rtnNamePro;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TreeNode node = new TreeNode(this.RepalcePrevDir(Request.ApplicationPath), Request.ApplicationPath);
node.SelectAction = TreeNodeSelectAction.Expand;
tvLocalDirFile.Nodes.Add(node);
// 这里要移除根路径不是 / 的那些字符
ListAllPath(Request.ApplicationPath.Remove(1), tvLocalDirFile.Nodes[0]);
// 初始化传入的值
SplitIdAndValue();
}
}
/// <summary>
/// 分隔传到页面的值的 id 和 value 值,方便传回调用页面
/// </summary>
private void SplitIdAndValue()
{
rtnNamePro = rtnIdPro = "value"; // 都只能是 value 属性
string[] rtnId;
string[] rtnName;
if (!string.IsNullOrEmpty(Request["rtnText"]))
{
rtnId = Request["rtnText"].ToString().Split('.'); // 接收返回值的对象ID和属性
if (rtnId.Length == 2) // 将objectname.value 格式的传入参数,分解为对象名和属性两个字符串
{
rtnIdObj = rtnId[0];
rtnIdPro = rtnId[1];
}
}
if (!string.IsNullOrEmpty(Request["rtnValue"]))
{
rtnName = Request["rtnValue"].ToString().Split('.');
if (rtnName.Length == 2)
{
rtnNameObj = rtnName[0];
rtnNamePro = rtnName[1];
}
}
}
/// <summary>
/// 转换成相对路径
/// </summary>
/// <param name="imagesurl1">虚拟目录</param>
/// <returns></returns>
private string urlconvertor(string imagesurl1)
{
string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());// 获取程序根目录
string imagesurl2 = imagesurl1.Replace(tmpRootDir, ""); // 转换成相对路径
imagesurl2 = imagesurl2.Replace(@"\", @"/");
//imagesurl2 = imagesurl2.Replace(@"Aspx_Uc/", @"");
return imagesurl2;
}
// 转换成绝对路径
private string urlconvertorlocal(string imagesurl1)
{
string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());// 获取程序根目录
string imagesurl2 = tmpRootDir + imagesurl1.Replace(@"/", @"\"); // 转换成绝对路径
return imagesurl2;
}
/// <summary>
/// 列出网站根目录
/// </summary>
/// <param name="dirPath"></param>
/// <param name="passNode"></param>
private void ListAllPath(string dirPath, TreeNode passNode)
{
string[] dirs;
string[] files;
//Response.Write(VirtualPathUtility.GetDirectory(Request.ApplicationPath));
dirs = Directory.GetDirectories(this.urlconvertorlocal(dirPath));
files = Directory.GetFiles(this.urlconvertorlocal(dirPath));
for (int i = 0; i < dirs.Length; i++)
{
TreeNode node = new TreeNode(this.RepalcePrevDir(this.urlconvertor(dirs[i])), dirs[i]);
//node.NavigateUrl = "#";
node.SelectAction = TreeNodeSelectAction.Expand;
//node.SelectAction= TreeNodeSelectAction.
passNode.ChildNodes.Add(node); // 已经添加了节点
ListAllPath(this.urlconvertor(dirs[i]), passNode.ChildNodes[passNode.ChildNodes.IndexOf(node)]); // 索引到那个节点
}
for (int i = 0; i < files.Length; i++)
{
TreeNode tNode = new TreeNode(this.RepalcePrevDir(this.urlconvertor(files[i])), files[i]);
tNode.NavigateUrl = "javascript:SetTextValue('" + this.urlconvertor(files[i]) + "', '" + files[i].Replace("\\", "\\\\") + "')";
passNode.ChildNodes.Add(tNode);
}
}
/// <summary>
/// 去除掉路径前的所有父路径
/// </summary>
/// <param name="dir"></param>
/// <returns></returns>
private string RepalcePrevDir(string dir)
{
return dir.Substring(dir.LastIndexOf("/") + 1);
}
相关文章推荐
- C#获取IIS所有站点及虚拟目录和应用程序(包含名称及详细信息)
- 遍历指定目录下所有子目录和子文件的函数
- php遍历目录输出目录及其下的所有文件示例
- 如何使用perl遍历一个目录下的所有文件
- PHP遍历并打印指定目录下所有文件
- 遍历目录下面所有的文件夹和文件
- 遍历目录下所有wav并播放
- (转)PHP自定义遍历目录下所有文件dir(),readdir()函数
- 遍历获取目录所有文件的md5
- 遍历指定目录下的所有目录和文件的php代码
- win2003服务器中创建Web网站虚拟目录的图文方法
- PHP遍历某个目录下的所有文件和子文件夹的实现代码
- 如何利用Java遍历所有的指定文件目录下的所有文件
- java遍历目录下的包括子目录下的所有文件
- Asp.net网站的ClickOnce自动部署(3)-虚拟目录的配置
- 虚拟目录部署网站母版页中引用文件方法推荐
- Python实现遍历目录下所有文件并写到文件中
- 网站安装打包 新建网站[四][虚拟目录及其它方法] 下
- 配置子目录Web.config使其消除继承,用虚拟目录创建多个网站的方法
- 遍历指定目录下所有文件