您的位置:首页 > 运维架构 > 网站架构

遍历虚拟网站下所有目录

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: