ASP.NET编程实例ABC(1)
点击次数:24 次 发布日期:2008-11-27 00:07:17 作者:源代码网
|
源代码网推荐 源代码网推荐 ASP.NET是微软.NET宏大战略的一部分。我们不能仅仅将它看成是ASP语言的进化版本,它所能发挥的威力将使开发人员激动与惊喜,更快、更好地实现梦想!本文就介绍ASP.NET的几个常用实例,通过与ASP语言实现方式的对比,让我们对它的强大眼见为实!实例内容(包括下载代码)如下: 源代码网推荐 源代码网推荐 ● 在ASP.NET中实现目录浏览 源代码网推荐 ● 在ASP.NET中随意创建图形信息 源代码网推荐 ● 在ASP.NET中显示事件日志记录 源代码网推荐 源代码网推荐 要运行这些例程,需要在Web服务器上安装Microsoft .NET Framework SDK。要理解代码的含义,需要对C# 编程语言有一定程度的了解。 源代码网推荐 源代码网推荐 在ASP.NET中实现目录浏览 源代码网推荐 源代码网推荐 在用ASP语言编程的"美好旧时光"中,我们应该不会忘记FileSystemObject对象。它允许对文件系统进行读、写以及目录浏览。但是这个对象模型并不十分完善,比如对于二进制文件来说,还没有开始操作就已经到达范围的结尾了。另外,还有一些希望的函数和属性在ASP中也没有具备。 源代码网推荐 但是出现了ASP.NET,这些愿望就都能在System.IO 名称空间中被 .NET Framework 类所实现。你会发现,文件处理从没有这么容易过,而且最重要的是从没有这么强大过。 源代码网推荐 这里,我们将演示文件处理的一个部分:文件和目录列表。我们创建了一些脚本程序,你可以将其应用在自己的应用程序中。 源代码网推荐 列举驱动器 源代码网推荐 首先,我们要知道在计算机上有哪些可浏览的驱动器,这些代码保存在文件listdrives.aspx中: 源代码网推荐 <% @Page Language="C#" %> 源代码网推荐 <% @Import Namespace="System.IO" %> 源代码网推荐 <% 源代码网推荐 string[] achDrives = Directory.GetLogicalDrives(); 源代码网推荐 int nNumOfDrives = achDrives.Length; 源代码网推荐 源代码网推荐 Response.Write("<ul>"); 源代码网推荐 for (int i=0; i < nNumOfDrives; i++) 源代码网推荐 { 源代码网推荐 Response.Write("<li><a href="listdir.aspx?dir="); 源代码网推荐 Response.Write(Server.UrlEncode(achDrives[i])); 源代码网推荐 Response.Write("">" + achDrives[i]); 源代码网推荐 Response.Write("</a><br>"); 源代码网推荐 } 源代码网推荐 Response.Write("</ul>"); 源代码网推荐 %> 源代码网推荐 根据 @Import 名称空间标识,在System.IO 名空间中有一个类叫做Directory,它包含了一些可用于目录上的功能。这个类中还包含静态(不需要对象例示就能调用的)方法GetLogicalDrives,它提供了包含驱动器字母的一个字符串数列。 源代码网推荐 当然,不能只有一个从GetLogicalDrives 获取的驱动器简单列表,还需要增加了一个页面,在这个页面中提供有关驱动器根目录的信息。 源代码网推荐 列出目录和文件 源代码网推荐 有了目录,我们就可以进一步探索子目录、文件以及目录本身的属性(如创建的日期等)了。文件 listdir.aspx 演示了如何执行这些功能: 源代码网推荐 <% @Page Language="C#" %> 源代码网推荐 <% @Import Namespace="System.IO" %> 源代码网推荐 <% 源代码网推荐 string strDir2List = Request.QueryString.Get("dir"); 源代码网推荐 Directory thisOne = null; 源代码网推荐 try 源代码网推荐 { 源代码网推荐 thisOne = new Directory(strDir2List); 源代码网推荐 // Reading the directory properties 源代码网推荐 Response.Write("<p>Creation: " + 源代码网推荐 thisOne.CreationTime.ToString() + "</p>"); 源代码网推荐 Directory[] subDirectories = thisOne.GetDirectories(); 源代码网推荐 Response.Write("<ul>"); 源代码网推荐 for (int i=0; i < subDirectories.Length; i++) 源代码网推荐 { 源代码网推荐 Response.Write("<li><a href="listdir.aspx?dir="); 源代码网推荐 Response.Write(Server.UrlEncode(subDirectories[i].FullName)); 源代码网推荐 Response.Write("">" + subDirectories[i].Name); 源代码网推荐 Response.Write("</a><br>"); 源代码网推荐 } 源代码网推荐 Response.Write("</ul>"); 源代码网推荐 源代码网推荐 File[] theFiles = thisOne.GetFiles(); 源代码网推荐 Response.Write("<ul>"); 源代码网推荐 for (int i=0; i < theFiles.Length; i++) 源代码网推荐 { 源代码网推荐 Response.Write("<li><a href="showfile.aspx?file="); 源代码网推荐 Response.Write(Server.UrlEncode(theFiles[i].FullName)); 源代码网推荐 Response.Write("">" + theFiles[i].Name); 源代码网推荐 Response.Write("</a><br>"); 源代码网推荐 } 源代码网推荐 Response.Write("</ul>"); 源代码网推荐 } 源代码网推荐 catch (Exception e) 源代码网推荐 { 源代码网推荐 Response.Write("Access not possible, error: <i>"); 源代码网推荐 Response.Write(e.ToString() + "</i>"); 源代码网推荐 Response.End(); 源代码网推荐 } 源代码网推荐 %> 源代码网推荐 我们使用GetDirectories方法实现目录信息的读取。这个方法返回一个目录对象数组,我们可以用这个数组来建立想要的功能,比如说建立一个更深入的链接。这同样适用于GetFiles方法,它返回一个文件对象数组。 源代码网推荐 你可能注意到这里使用了try-catch语句。是的,这是为了防止例外情况的发生。比如,当用户试图访问一些不允许他访问的内容时。 源代码网推荐 显示文件信息 源代码网推荐 现在到了文件这一级。一个文件有许多属性,为了缩短脚本,下面的例程(文件showfile.aspx)只显示了其中的一些: 源代码网推荐 <% @Page Language="C#" %> 源代码网推荐 <% @Import Namespace="System.IO" %> 源代码网推荐 <html> 源代码网推荐 <head><title>File Info</title></head> 源代码网推荐 <body> 源代码网推荐 <% 源代码网推荐 string strFile2Show = Request.QueryString.Get("file"); 源代码网推荐 File thisOne = new File(strFile2Show); 源代码网推荐 %> 源代码网推荐 <table> 源代码网推荐 <tr><td>Name:</td><td><%=thisOne.Name%></td></tr> 源代码网推荐 <tr><td>Path:</td><td><%=thisOne.FullName%></td></tr> 源代码网推荐 <tr><td>Directory:</td><td><%=thisOne.DirectoryName%></td></tr> 源代码网推荐 <tr> 源代码网推荐 <td>Date created:</td> 源代码网推荐 <td><%=thisOne.CreationTime.ToString()%></td> 源代码网推荐 </tr> 源代码网推荐 <tr> 源代码网推荐 <td>Size:</td> 源代码网推荐 <td><%=thisOne.Length.ToString()%> Bytes</td> 源代码网推荐 </tr> 源代码网推荐 <tr> 源代码网推荐 <td>Last access:</td> 源代码网推荐 <td><%=thisOne.LastAccessTime.ToString()%></td> 源代码网推荐 </tr> 源代码网推荐 <tr> 源代码网推荐 <td>Last modified:</td> 源代码网推荐 <td><%=thisOne.LastWriteTime.ToString()%></td> 源代码网推荐 </tr> 源代码网推荐 </table> 源代码网推荐 源代码网推荐 <% 源代码网推荐 StreamReader theReader = thisOne.OpenText(); 源代码网推荐 char[] theBuffer = new char[255]; 源代码网推荐 int nRead = theReader.ReadBlock(theBuffer, 0, 255); 源代码网推荐 Response.Write("<pre>"); 源代码网推荐 Response.Write(Server.HtmlEncode(new String(theBuffer,0,nRead))); 源代码网推荐 Response.Write("</pre>"); 源代码网推荐 %> 源代码网推荐 源代码网推荐 </body> 源代码网推荐 </html> 源代码网推荐 为了演示实现的简单性,在这段脚本代码的结尾处抄了个近路执行文件的读取。首先,打开一个 StreamReader 对象,创建一个缓冲器,用ReadBlock方法来填充缓冲器,并且将 HTML编码段传递给客户;然后很快地就形成了一个小型的"文件预览"。没有费多大周折,你看操作文件的内容是多么得简单 :-) 源代码网推荐 递归循环以显示目录列表 源代码网推荐 最后,我们还要编写一个创建目录树的程序recursivelisting.aspx。在其中,我们使用函数RecursiveDump来生成目录树。请看具体代码: 源代码网推荐 <% @Page Language="C#" %> 源代码网推荐 <% @Import Namespace="System.IO" %> 源代码网推荐 <% 源代码网推荐 string strDir2List = Request.QueryString.Get("dir"); 源代码网推荐 Directory thisOne = new Directory(strDir2List); 源代码网推荐 源代码网推荐 Response.Write("<pre>"); 源代码网推荐 RecursiveDump(thisOne, 0); 源代码网推荐 Response.Write("</pre>"); 源代码网推荐 %> 源代码网推荐 源代码网推荐 <script language="C#" runat="server"> 源代码网推荐 void RecursiveDump(Directory theDir, int nLevel) 源代码网推荐 { 源代码网推荐 Directory[] subDirectories = theDir.GetDirectories(); 源代码网推荐 for (int i=0; i < subDirectories.Length; i++) 源代码网推荐 { 源代码网推荐 Response.Write(new String(" ",nLevel)); 源代码网推荐 Response.Write(subDirectories[i].Name + " "); 源代码网推荐 RecursiveDump(subDirectories[i], nLevel+1); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 </script> 源代码网推荐 为了简单起见,这里只使用了空格来生成目录树结构。为使空格在HTML中可以生效,我们必须使用<PRE>标记,否则这些空格就会被忽视。 源代码网推荐 函数本身有两个参数:一个是目录对象,一个是整数变量以指示当前级别。在当前级别中,用 GetDirectories读取子目录信息,然后在一个 for循环中显示它。对于每个Directory对象, 都调用 RecursiveDump函数,然后级别加1。 源代码网推荐 注意:列举整个驱动器会花费很多时间。最后,让我们来看看浏览器中的执行结果: 源代码网推荐 源代码网推荐 源代码网推荐 小 结 源代码网推荐 本文介绍了System.IO 名称空间中的两个对象:Directory和 File。虽然"只"用它们简单显示了目录和文件信息,但我们要知道:这两个对象是执行文件处理的基础。 源代码网推荐 源代码网推荐 源代码网供稿. |
