zoukankan      html  css  js  c++  java
  • 遍历虚拟网站下所有目录

    前台页面

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        
    <title>选择本地路径</title>
        
    <base target="_self" />

        
    <script src="http://www.cnblogs.com/js/jquery-1.4.2.js" type="text/javascript"></script>
        
    <script language="javascript" type="text/javascript">
            
            
    var _text; // 返回的名
            var _value; // 返回的值
            
            
    function NoTrans(obj){
                
    return obj.replace("\\","\\\\");
            }
            
            
    // div 出现在右侧,方便用户选择
            $(document).ready(function(){
                $(
    "a").click(function(){
                    
    var obj = $(this);
                    
                    
    var offset = obj.offset();
                    
    var right = offset.left + obj.width();
                    
    var down = offset.top + obj.height();
                   
                    $(
    "#dSubmit").css({'top':down,'left':right + 50});
                })
            });
            
            
    function SetTextValue(text, value){
                _text 
    = text;
                _value 
    = value;
            }
            
            
    function SetValueAndReturn(){
                
            }
        
    </script>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
            
    <div>
                
    <asp:TreeView ID="tvLocalDirFile" runat="server" ImageSet="XPFileExplorer" 
                    NodeIndent
    ="15" ExpandDepth="1">
                    
    <ParentNodeStyle Font-Bold="False" />
                    
    <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
                    
    <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" 
                        HorizontalPadding
    ="0px" VerticalPadding="0px" />
                    
    <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" 
                        HorizontalPadding
    ="2px" NodeSpacing="0px" VerticalPadding="2px" />
                
    </asp:TreeView>
            
    </div>
        
    <div id="dSubmit" align="center" style="position:absolute; border: solid 1 #462375">
            
    <input type="button" id="btnOK" value="确定" onclick="returnValue();" />&nbsp;&nbsp;
            
    <input type="button" id="btnCancel" value="取消" onclick="window.close();" />
        
    </div>
        
    <script language="javascript" type="text/javascript">
            
    // 返回的方法
            function returnValue() {
                
    var opener = null;
                
    try{
                    opener 
    = window.dialogArguments;
                }
                
    catch(e) { }
                
    if(opener == null)   
                {
                    opener 
    = window.opener;
    ;
                }
                
    try { 
                    opener.document.getElementById(
    "<%= rtnIdObj%>").<%= rtnIdPro%>= _text;
                } 
                
    catch (e){ }
                
    try {
                    opener.document.getElementById(
    "<%= rtnNameObj%>").<%=rtnNamePro %> = _value;
                }
                
    catch(e){ } 
                
    try
                {
                    
    <%=string.IsNullOrEmpty(Request.QueryString["callbackfn"]) ? "" : "opener." + Request.QueryString["callbackfn"+ "();"%>
                }
                
    catch(e){}
                window.close();
            }
        
    </script>
        
    </div>
        
    </form>
    </body>
    </html>
    后台代码
        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); 
        }

    走向地狱的途中,不小心走了程序员这条路,路上一个个黑心的老板,和暗无天日的加班,我才发现,通往地狱的路径中,我们这行是最短的。

  • 相关阅读:
    MySQL基础
    DNS域名解析服务
    仿Mars MP3播放器项目5
    仿Mars MP3播放器项目4
    仿Mars MP3播放器项目3
    仿Mars MP3播放器项目2
    仿Mars MP3播放器项目1
    JAVA 随记1
    Redis和Memcache的区别
    php用smtp发送邮件
  • 原文地址:https://www.cnblogs.com/zlfucku/p/1696674.html
Copyright © 2011-2022 走看看