X龙@China .Net 'blog

需要的不仅仅是工作,而是通过努力得来的美好将来。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

web下的目录选择

Posted on 2009-02-13 09:45  X龙  阅读(329)  评论(0编辑  收藏  举报

 

代码
using System;
using System.Web;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Web.UI;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;

public partial class FileList:System.Web.UI.Page
{
    
public void Page_Load(object sender, System.EventArgs e)
    {
        
try
        {
            
//获取驱动器
            string[] drives = System.IO.Directory.GetLogicalDrives();
            DiskList.Items.Add(
"选择驱动器");
            
foreach (string str in drives) { DiskList.Items.Add(str); }

            
string strCurrentDir, NewFolder, TempAction;
            NewFolder 
= Request.QueryString["NewFolder"];
            TempAction 
= Request.QueryString["Action"];



            
//分情况显示数据
            if (TempAction == null && NewFolder == null)
            {
                strCurrentDir 
= Server.MapPath(".");                
                DisplayList(strCurrentDir);
            }
            
else if (TempAction == "up")
            {
                strCurrentDir 
= NewFolder;
                UpFolder(strCurrentDir);
            }
            
else if (TempAction == "info")
            {
                SysInfo();
            }
            
else
            {
                strCurrentDir 
= NewFolder;
                DisplayList(strCurrentDir);
            }
        }
        
catch (System.IO.IOException ex)
        {
            Alert(ex.Message);
            ClientScript.RegisterStartupScript(
this.GetType(), """window.history.go(-1)"true);
            
return;
        }
        
catch (System.Security.SecurityException)
        {
            ErrorText.Text 
= "The caller does not have the required permission.";
        }
        
catch (Exception ex)
        {
            ErrorText.Text 
= ex.Message;
        }
    }

    
#region Files and Directorys List
    
/// <summary>
    
/// 显示文件和文件夹列表。
    
/// </summary>
    public void DisplayList(string strCurrentDir)
    {
        strCurrentDir 
= strCurrentDir.Replace(@""@":").Replace(@""@"\").Replace(@"\\"@"\");

        StringBuilder TempStr 
= new StringBuilder();
        TempStr.Capacity 
= 100;
        
string dirName,dirIcon;
        
int subDirNum=0;
        DateTime dirModifyData;

        fileShow.Text 
= "";//初始化显示层

        
if (!Directory.Exists(strCurrentDir))
        {
            
throw new IOException("目录不存在。");
        }

        
//设置选择的磁盘分区
        string partZoneName = strCurrentDir.Substring(0, strCurrentDir.IndexOf("\\")+1);
        
for (int i = 0; i < DiskList.Items.Count; i++)
        {
            
if (DiskList.Items[i].Text == partZoneName)
            {
                DiskList.Items[i].Selected 
= true;
                
break;
            }
        }
        
        DirectoryInfo curDirInfo 
= new DirectoryInfo(strCurrentDir);

        
if (curDirInfo.GetDirectories().Length < 1)
        {
            
throw new IOException("此目录中没有子目录!");
        }
        
foreach (DirectoryInfo dir in curDirInfo.GetDirectories())
        {
            subDirNum
++;
            dirName 
= dir.Name;
            dirModifyData 
= dir.LastWriteTime;
            
            dirIcon 
= "<font face='wingdings' color='#666666' style='font-size:30'>0</font>";
            
string divContent="<div class=imagespacer><div class=imageholder onclick=\"SelectDiv(this,'" + dirName.Replace(" ", "&nbsp;").ToString() + "','" +dirModifyData.ToLongDateString() +"');\""+
                                                                           
" ondblclick=\"OpenObject('" + Server.UrlEncode(dir.Name)+"');\">" + dirIcon + "</div>"+
                              
"<div class=titleholder>" + dir.Name + "</div></div>";
            TempStr.Append(divContent);

        }
        fileShow.Text 
= TempStr.ToString();      
        FolderPath.Value 
= strCurrentDir;
        FolderUrl.Value 
= Server.UrlEncode(strCurrentDir);
        ObjectNum.Value 
= curDirInfo.GetDirectories().Length.ToString();
       
        leftpic.Text 
= "<b><font face='wingdings' color='#6699CC' style='font-size:30'>;</font></b>";
        
//ListImage.ImageUrl = "images/left_disk.gif";
        ListLabel.Text = Directory.GetDirectoryRoot(strCurrentDir);
        goup.Enabled 
= false;
        
//IMG1.Src = "images/turnup1.gif";
        
    }
    
#endregion
    
/// <summary>
    
/// 获取当前文档名。
    
/// </summary>
    private string GetFileName()
    {
        Uri nUrl 
= HttpContext.Current.Request.Url;
        
string sFileName = nUrl.Segments[nUrl.Segments.Length - 1];
        
int i = sFileName.LastIndexOf("/");
        
int j = sFileName.Length;
        sFileName 
= sFileName.Substring(i + 1, j - i - 1);
        
return sFileName;
    }
    
/// <summary>
    
/// 系统信息。
    
/// </summary>
    private void SysInfo()
    {
        Response.Write(
"<html><head><meta http-equiv='Content-Type' content='text/html; charset=gb2312'><title>DiskExpert V2.0 Power By Pot</title><style>td{font-size:14px}</style></head><body><table width='80%'  border='1' align='center' cellpadding='2' style='border-collapse: collapse;'><tr><td colspan='2'><font color='#FF0000'>Web服务器信息</font></td></tr><tr><td width='40%'>服务器IP</td><td width='60%'>" + Request.ServerVariables["LOCAL_ADDR"+ "</td></tr><tr><td>机器名</td><td>" + Environment.MachineName + "</td></tr><tr><td>网络域名</td><td>" + Environment.UserDomainName.ToString() + "</td></tr><tr><td>当前进程的用户名</td><td>" + Environment.UserName + "</td></tr><tr><td>操作系统</td><td>" + Environment.OSVersion.ToString() + "</td></tr><tr><td>IIS版本</td><td>" + Request.ServerVariables["SERVER_SOFTWARE"+ "</td></tr><tr><td colspan='2'><font color='#FF0000'>客户端信息</font></td></tr><tr><td>客户端IP</td><td>" + Request.ServerVariables["REMOTE_ADDR"+ "</td></tr><tr><td>用户标识</td><td>" + Request.ServerVariables["HTTP_USER_AGENT"+ "</td></tr></table></body></html>");
        Response.End();
    }
    
/// <summary>
    
/// 返回上一级。
    
/// </summary>
    private void UpFolder(string strCurrentDir)
    {
        
string UpPath = new FileInfo(strCurrentDir).DirectoryName;
        DisplayList(UpPath);
        FolderPath.Value 
= UpPath;
        FolderUrl.Value 
= Server.UrlEncode(UpPath);
        
return;
    }

    
public void Alert(string msg)
    {
        
this.ClientScript.RegisterClientScriptBlock(this.GetType(), """alert(\"" + Regex.Replace(msg.Replace("\"""'"), @"\W""", RegexOptions.Multiline) + "\");", true);
    }
}

 

 

我们在做b/s软件时,有时需要选择服务器目录,在此共享一个服务器目录选择方法:

页面代码如下:

 

 

代码
<%@ Page Language="C#" EnableViewState="false" ValidateRequest="false" Inherits="FileList"
    CodeBehind
="FileList.aspx.cs" AutoEventWireup="True" %>

<html>
<head>
    
<title>DiskExpert V2.0 Power by Pot</title>
    
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    
<style type="text/css">
        A
        
{
            cursor
: hand;
        
}
        A:visited
        
{
            text-decoration
: none;
            color
: 000000;
        
}
        A:hover
        
{
            text-decoration
: none;
        
}
        A:link
        
{
            text-decoration
: none;
            color
: 000000;
        
}
        BODY
        
{
            font-family
: Tahoma;
            font-size
: 8pt;
            scrollbar-face-color
: #ffffff;
            scrollbar-highlight-color
: #acb0b5;
            scrollbar-shadow-color
: #acb0b5;
            scrollbar-3dlight-color
: #ffffff;
            scrollbar-arrow-color
: #acb0b5;
            scrollbar-track-color
: #ffffff;
            scrollbar-darkshadow-color
: #ffffff;
            scrollbar-base-color
: #acb0b5;
        
}
        TD
        
{
            font-family
: Tahoma;
            font-size
: 8pt;
        
}
        DIV
        
{
            cursor
: default;
            font-family
: Tahoma;
            font-size
: 8pt;
        
}
        select
        
{
            border-width
: 1;
            border-color
: #000000;
            background-color
: #efefef;
            font-family
: Tahoma;
            font-size
: 8pt;
            font-style
: bold;
        
}
        .botton
        
{
            font-size
: 8pt;
            background
: url(http://music.myule.com/images/botton_01.gif) no-repeat right bottom;
            font-family
: Tahoma;
            color
: #7E8083;
            border
: 1px solid #ACB0B5;
        
}
        .botton1
        
{
            font-size
: 8pt;
            font-family
: Tahoma;
            border
: 1px solid #ACB0B5;
        
}
        INPUT
        
{
            border-top-width
: 1px;
            padding-right
: 1px;
            padding-left
: 1px;
            border-left-width
: 1px;
            font-size
: 8pt;
            border-left-color
: #cccccc;
            border-bottom-width
: 1px;
            border-bottom-color
: #cccccc;
            padding-bottom
: 1px;
            border-top-color
: #cccccc;
            padding-top
: 1px;
            height
: 18px;
            border-right-width
: 1px;
            border-right-color
: #cccccc;
            font-family
: Tahoma;
        
}
        DIV.imagespacer
        
{
            float
: left;
            margin
: 5px;
            overflow
: hidden;
            width
: 90px;
            padding-top
: 0px;
            height
: 90px;
            text-align
: center;
        
}
        DIV.imageholder
        
{
            border-right
: #cccccc 1px solid;
            padding-right
: 0px;
            border-top
: #cccccc 1px solid;
            padding-left
: 0px;
            padding-bottom
: 0px;
            margin
: 0px;
            vertical-align
: middle;
            border-left
: #cccccc 1px solid;
            width
: 60px;
            padding-top
: 15px;
            border-bottom
: #cccccc 1px solid;
            height
: 60px;
            text-align
: center;
        
}
        DIV.titleHolder
        
{
            width
: 100%;
            padding-top
: 2px;
            height
: 20px;
            text-align
: center;
        
}
        .style1
        
{
            font-size
: medium;
        
}
        .style2
        
{
            color
: #CC0000;
            font-size
: small;
        
}
        .style4
        
{
            color
: #CC0000;
        
}
    
</style>
</head>
<body style="overflow: auto" bgcolor="#e4f4ff" leftmargin="0" topmargin="0">
    
<div style="position: absolute; left: 0; top: 0; width: 100%; height: 100%; z-index: 1">
        
<table width="98%" height="100%" border="0" align="center" cellpadding="0" cellspacing="0">
            
<tr>
                
<td valign="top" height="45">
                    
<table cellspacing="0" cellpadding="0" width="100%" border="0">
                        
<form id="form1" name="form1" method="get" action="">
                        
<tr>
                            
<td style="color: #7e8083" height="20">
                                
<div id="msviLocalToolbar">
                                    
<table style="cursor: default" cellspacing="0" cellpadding="0" border="0">
                                        
<tr>
                                            
<td align="left">
                                                
<br />
                                                
<input id="ObjectNum" type="hidden" name="Hidden2" runat="server" class="style1"/>
                                                
<input id="FolderUrl" type="hidden" name="hidden" runat="server" class="style1"/>
                                                
<input id="FolderPath" type="hidden" name="FolderPath" runat="server" class="style1"/>
                                                
<span class="style1">请选择您希望将数据还原到的磁盘位置: </span><span class="style2">
                                                    
<br />
                                                
</span><span class="style4">(*注意:请确保在地址栏中的目录是存在的)</span>
                                            
</td>
                                        
</tr>
                                    
</table>
                                
</div>
                            
</td>
                        
</tr>
                        
<tr>
                            
<td height="1" align="right" bgcolor="#ACB0B5">
                            
</td>
                        
</tr>
                        
<tr>
                            
<td height="1" align="right" bgcolor="#F7FAFF">
                            
</td>
                        
</tr>
                        
<tr>
                            
<td align="right">
                                
<table cellspacing="0" cellpadding="0" width="100%" border="0">
                                    
<tr>
                                        
<td align="center" width="180">
                                            
<div id="msviLocalToolbar1">
                                                
<table cellspacing="0" cellpadding="0" width="95%" border="0">
                                                    
<tr align="center">
                                                        
<td>
                                                            
<font color="#cecfd0">|</font>
                                                        
</td>
                                                        
<td>
                                                        
</td>
                                                        
<td class="lt0" width="18%">
                                                            
<onclick="window.location.reload();return false;" href="#">刷新</a>
                                                        
</td>
                                                        
<td>
                                                            
<font color="#cecfd0">|</font>
                                                        
</td>
                                                        
<td>
                                                        
</td>
                                                        
<td width="18%" height="20">
                                                            
<onclick="history.go(-1)" href="#">后退</a>
                                                        
</td>
                                                        
<td>
                                                            
<font color="#cecfd0">|</font>
                                                        
</td>
                                                        
<td width="18%">
                                                            
<asp:HyperLink ID="goup" runat="server">向上</asp:HyperLink>
                                                        
</td>
                                                        
<td>
                                                            
<font color="#cecfd0">|</font>
                                                        
</td>
                                                    
</tr>
                                                
</table>
                                            
</div>
                                        
</td>
                                        
<td width="30">
                                            
<font color="#7e8083">地址:</font>
                                        
</td>
                                        
<td>
                                            
<input id="NewFolder" style="width: 100%" type="text" name="text" runat="server"/>
                                        
</td>
                                        
<td width="25">
                                            
<input type="submit" name="Submit" value="→"/>
                                        
</td>
                                        
<td width="90">
                                            
<select id="DiskList" onchange="window.location=('?NewFolder='+options[selectedIndex].value)"
                                                name
="Select1" runat="server">
                                            
</select>
                                        
</td>
                                        
<td width="30">
                                        
</td>
                                    
</tr>
                                
</table>
                            
</td>
                        
</tr>
                        
<tr>
                            
<td height="2" align="right" bgcolor="#ACB0B5">
                                
<img height="1" src="images/spacer.gif" width="1">
                            
</td>
                        
</tr>
                        
</form>
                    
</table>
                
</td>
            
</tr>
            
<tr>
                
<td valign="top" bgcolor="#ffffff">
                    
<table height="100%" cellspacing="0" cellpadding="0" width="100%" border="0">
                        
<tr>
                            
<td valign="top" width="200">
                                
<div style="overflow: auto; width: 200px; height: 100%">
                                    
<table cellspacing="0" cellpadding="0" width="100%" border="0">
                                        
<tr>
                                            
<td valign="top" height="76">
                                                
<table cellspacing="0" cellpadding="0" width="100%" border="0">
                                                    
<tr>
                                                        
<td width="10" height="50">
                                                            
&nbsp;
                                                        
</td>
                                                        
<td valign="top" align="left">
                                                            
<table cellspacing="0" cellpadding="0" width="100%" border="0">
                                                                
<tr>
                                                                    
<td valign="bottom" height="40">
                                                                        
<asp:Label ID="leftpic" runat="server"></asp:Label>
                                                                    
</td>
                                                                
</tr>
                                                            
</table>
                                                        
</td>
                                                    
</tr>
                                                    
<tr>
                                                        
<td width="10">
                                                            
<img height="1" src="images/spacer.gif" width="10">
                                                        
</td>
                                                        
<td style="font-weight: bold; font-size: 14pt; line-height: 1.1; font-family: Tahoma"
                                                            valign
="bottom">
                                                            
<asp:Label ID="ListLabel" runat="server"></asp:Label>
                                                        
</td>
                                                    
</tr>
                                                    
<tr>
                                                        
<td width="10" height="10">
                                                            
<hr size="2" color="#6699CC" noshade>
                                                        
</td>
                                                        
<td>
                                                            
<hr size="2" color="#6699CC" noshade>
                                                        
</td>
                                                    
</tr>
                                                
</table>
                                            
</td>
                                        
</tr>
                                        
<tr>
                                            
<td valign="top">
                                                
<table cellspacing="0" cellpadding="0" width="100%" border="0">
                                                    
<tr>
                                                        
<td width="10" height="25">
                                                            
<img height="1" src="images/spacer.gif" width="10">
                                                        
</td>
                                                        
<td>
                                                            
&nbsp;
                                                        
</td>
                                                    
</tr>
                                                    
<tr>
                                                        
<td width="10" height="40">
                                                            
&nbsp;
                                                        
</td>
                                                        
<td>
                                                            
<span id="lblObjDir" style="font-size: 8pt; line-height: 1.3; font-family: Tahoma">
                                                            
</span>
                                                        
</td>
                                                    
</tr>
                                                    
<tr>
                                                        
<td width="10">
                                                            
&nbsp;
                                                        
</td>
                                                        
<td>
                                                            
<span id="fileinfo" style="font-size: 8pt; font-family: Tahoma"></span>
                                                        
</td>
                                                    
</tr>
                                                    
<tr>
                                                        
<td height="20">
                                                            
&nbsp;
                                                        
</td>
                                                        
<td>
                                                            
&nbsp;
                                                        
</td>
                                                    
</tr>
                                                
</table>
                                            
</td>
                                        
</tr>
                                    
</table>
                                
</div>
                            
</td>
                            
<td onselectstart="return false;" onclick="return BodyClick()" valign="top">
                                
<div style="overflow: auto; width: 100%; height: 100%">
                                    
<font face="宋体">
                                        
<asp:Literal ID="fileShow" runat="server"></asp:Literal></font></div>
                            
</td>
                        
</tr>
                    
</table>
                
</td>
            
</tr>
            
<tr>
                
<td height="1" bgcolor="#ACB0B5">
                
</td>
            
</tr>
            
<tr>
                
<td height="1" valign="top" bgcolor="#F7FAFF" style="color: red">
                    
<asp:Label ID="ErrorText" runat="server"></asp:Label>
                
</td>
            
</tr>
            
<tr>
                
<td valign="top" height="45">
                    
<table cellspacing="1" cellpadding="0" width="100%" border="0">
                        
<tr>
                            
<td>
                                
<table cellspacing="0" cellpadding="0" width="100%" border="0">
                                    
<form id="form2" name="form2" action="FileList.aspx" runat="server">
                                    
<tr>
                                        
<td height="30" align="center">
                                            
<input id="CurrentFile" type="hidden" name="CurrentFile" runat="server">
                                            
<input id="CurrentFolder" type="hidden" name="CurrentFolder" runat="server">
                                            
<input type="button" id="btnEnter" onclick="btnEnter_Click()" value="确定" style="width: 80px;
                                                height: 40px; font: 16pt;"
 />
                                        
</td>
                                    
</tr>
                                    
</form>
                                
</table>
                            
</td>
                        
</tr>
                        
<tr>
                            
<td>
                                
<table width="100%" border="0" cellspacing="0" cellpadding="0">
                                
</table>
                            
</td>
                        
</tr>
                    
</table>
                
</td>
            
</tr>
        
</table>
    
</div>
<script type="text/javascript">
var flag = false;
var lastDiv = null,FolderNum,ClickArea = 0;
var selectedDbDir = document.getElementById("NewFolder").value=document.getElementById("FolderPath").value;

window.status 
= "就绪"

FolderNum 
= form1.ObjectNum.value;
lblObjDir.innerHTML 
= "共计:&nbsp;"+FolderNum+"&nbsp;个目录<br><br>";

if(typeof form2.Button7!="undefined")
{
    form2.Button7.disabled 
= true;form2.Button8.disabled = true;
}    
document.form2.CurrentFolder.value 
= "";
document.form2.CurrentFile.value 
= "";

function BodyClick()
{
    
if(ClickArea == 1)
    {
        ClickArea 
= 0;
        
return;
    }
    
else
    {
        lblObjDir.innerHTML
="共计:&nbsp;"+FolderNum+"&nbsp;个目录<br><br>"
        fileinfo.innerHTML
="";           
        
if (lastDiv)
        {
            lastDiv.style.border 
= "1 solid #CCCCCC";
            lastDiv.style.background 
= "white";
        }
        document.form2.CurrentFolder.value
="";
        
if(typeof form2.Button7!="undefined")
        {
            form2.Button7.disabled 
= true;
            form2.Button8.disabled 
= true;
        }
        window.status 
= "就绪";return;
    }
}            

function SelectDiv(theDiv,dirName,lastModify)
{
    
//设置选择的文件夹
    selectedDbDir = document.getElementById("NewFolder").value+"\\"+dirName; 
    
if (lastDiv) 
    {
        lastDiv.style.border 
= "1 solid #CCCCCC";lastDiv.style.background = "white";
    }
    lastDiv 
= theDiv;
    theDiv.style.border 
= "1 solid #316AC5";
    theDiv.style.background 
= "#EBF0FA";
    document.form2.CurrentFolder.value 
= dirName;
    window.status 
= "文件夹「"+dirName+"」被选中!";
    lblObjDir.innerHTML 
= "<b>"+dirName+"</b><br>";
    fileinfo.innerHTML 
= "修改时间:&nbsp;"+lastModify+"<br><br>";
    ClickArea 
= 1;
    
if(typeof form2.Button7!="undefined"){form2.Button7.disabled = false;form2.Button8.disabled = false;}
    
return;
}        
function OpenObject(dirName)
{       
    window.location
=("?NewFolder="+form1.FolderUrl.value+"\\"+dirName);
}
function GetFileName(){var str = this.location.href;return str.match(/\/([^\?/]*)(\?|$)/)[1];}
function openeditor(){
if(form2.CurrentFile.value==""){alert("请选取一个文件!");return false;}
window.open(
"about:blank",'editor','scrollbars=1,menubar=0,status=0,width=700,height=500,left=50,top=110,resizable=1');window.open("?Action=edit&url="+form1.FolderUrl.value+"\\"+form2.CurrentFile.value,'editor','scrollbars=1,menubar=0,status=0,width=700,height=500,left=50,top=110,resizable=1');}

function btnEnter_Click()

    flag 
= true;
    
if(window.opener!=null)
    {       
        window.opener.getDBSavePath(selectedDbDir); 
        window.close();
    }
    
else
    {
        alert(
'操作错误');
    }
}
window.onunload
=function()
    {
        
if(!flag)
        {
            
if(window.opener!=null)
            {
                window.opener.getDBSavePath(selectedDbDir);
            }
        }
    }
</script>
</body>
</html>

 

 

点击这里给我发消息http://wp.qq.com/index.html