请教:要做个控件,显示服务器和客户端文件,类似windows资源管理器,给个思路

Roger313 2003-10-15 06:07:10
rt
...全文
33 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
gOODiDEA 2003-11-10
  • 打赏
  • 举报
回复
参考:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="Tutorial.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title></title>
<meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:dropdownlist id="DirList" style="Z-INDEX: 101; LEFT: 12px; POSITION: absolute; TOP: 55px" runat="server" Width="675px" Height="22px"></asp:dropdownlist>
<asp:listbox id="FileList" style="Z-INDEX: 102; LEFT: 10px; POSITION: absolute; TOP: 87px" runat="server" Width="224px" Height="467px"></asp:listbox>
<asp:listbox id="FileData" style="Z-INDEX: 103; LEFT: 241px; POSITION: absolute; TOP: 87px" runat="server" Width="695px" Height="465px"></asp:listbox>
<asp:Label id="CurrentPath" style="Z-INDEX: 104; LEFT: 19px; POSITION: absolute; TOP: 12px" runat="server" Width="852px" Height="25px">C:\</asp:Label>
</form>
<script language="vbscript">
' make the form submit (i.e. the page reload) when a file of folder is selected

Sub DirList_OnPropertyChange()
form1.FileList.selectedIndex = -1
form1.submit()
End Sub

Sub FileList_OnPropertyChange()
form1.submit()
End Sub
</script>
</body>
</HTML>




The .aspx.vb File
There are 2 main subs here (other than the page_load event handler) that do all the work:
1. LoadLists - populates the file and folder lists by enumerating through the contents of the selected folder with the DirectoryInfo.GetFileSystemInfos() method.

2. LoadFileData - reads the contents of a selected file into an ArrayList using the StreamReader object, then loops through the ArrayList to populate the FileData ListBox.


Imports System.IO
' don’t forget to import System.IO or you won’t be able to use
' DirectoryInfo, FileSystemInfo as StreamReader classes

Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents FileList As System.Web.UI.WebControls.ListBox
Protected WithEvents FileData As System.Web.UI.WebControls.ListBox
Protected WithEvents CurrentPath As System.Web.UI.WebControls.Label
Protected WithEvents DirList As System.Web.UI.WebControls.DropDownList

#Region " Web Form Designer Generated Code "
'<snip>
#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

Dim thisDir As String
Dim thisFile As String

If Page.IsPostBack Then
' we have access to the Request object
thisDir = Request("DirList")
thisFile = Request("FileList")
Else
thisDir = "C:\"
End If
If thisFile <> "" Then
' we know a file was selected because we get it’s name from
' the value posted to the page.
LoadFileData(thisFile, CurrentPath.Text)
Else
' no file was selected therefore a directory must
' have been selected, so load the new file and directory lists
' as per the contents of the selected directory.
LoadLists(thisDir)
End If

' make sure that nothing is already selected when the page loads.
DirList.SelectedIndex = -1
FileList.SelectedIndex = -1
End Sub

Sub LoadLists(ByVal thisDir As String)
' loop thru the directory and build the directory dropdownlist
' and the file list.
Try
Dim dir As DirectoryInfo = New DirectoryInfo(thisDir)
Dim fsi As FileSystemInfo
DirList.Items.Clear()
FileList.Items.Clear()
FileData.Items.Clear()
DirList.Items.Add("Select a subfolder...")
CurrentPath.Text = dir.FullName.ToString
For Each fsi In dir.GetFileSystemInfos()
Try
If Not TypeOf fsi Is FileInfo Then
' it must be a directory
Dim d As DirectoryInfo = CType(fsi, DirectoryInfo)
Dim dirName As String = Path.GetFullPath(d.FullName.ToString)
DirList.Items.Add(dirName)
Else
Dim d As FileInfo = CType(fsi, FileInfo)
Dim fileName As String = d.Name.ToString
FileList.Items.Add(fileName)
End If
Catch E As Exception
'skip this one and try the next...
End Try
Next fsi

Catch E As Exception
Response.Write(E.ToString())
Response.End()
End Try
End Sub

Sub LoadFileData(ByVal thisFile As String, ByVal thisDir As String)
' take the name of the current directory and the name of the
' selected file and open the file with a StreamReader object.
Try
FileData.Items.Clear()
Dim din As StreamReader = File.OpenText(thisDir & "\" & thisFile)
Dim str As String
Dim al As ArrayList = New ArrayList()


' Read the contents of the file into an arraylist then
' add each element of the arraylist to the FileData
' listbox.
Do
str = din.ReadLine()
If str <> Nothing Then
al.Add(str)
End If
Loop Until str = Nothing

Dim s As String

For Each s In al
FileData.Items.Add(s)
Next s

Catch E As Exception
Response.Write(E.ToString())
Response.End()
End Try
End Sub

End Class


redlion963 2003-11-09
  • 打赏
  • 举报
回复
问的有问题吧
xueqs 2003-11-09
  • 打赏
  • 举报
回复
没人会???

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧