发新话题
打印

ASP.NET如何操作文件

ASP.NET如何操作文件

本文由chenyangasp版权所有,可以转载,复制,粘贴,并请注明出处,但不得修改!
  在asp.net操作文件的所有concept都在system.io namespace中,这个namespace包含读写操作文件所必需的类。
  本文将详细介绍关于文件读,写,删除等asp.net中的文件操作。
  创建文件:并写入一些内容。
  正如开始我们提到的,我们需要在我们的asp.net的页面的前面添加文件操作的namespace“system.io” 第一步:
  如下所示添加namespace <%@ Import Namespace="System.IO" %>
   下一步制作文本文件
   writefile.aspx
  <%@ Import Namespace="System.IO" %>
  <%
  Response.write("Writing the content into Text File in ASP.NET <BR>")
   “ 声明streamwriter对象
  Dim strwriterobj As StreamWriter
   “ 创建文本文件并将其赋给上面声明的streamwriter对象
  strwriterobj= File.CreateText("c:\aspnet.txt" )
   “在刚才创建的文本文件里写一些东东
  strwriterobj.WriteLine( "Welcome to user chenyang“s ASP.NET Program" )
   strwriterobj.Close
   Response.write("创建文本文件并填充内容")
  %>
  现在我们完成了第一部分
   接下来,我们来完成第二部分
  从文件中读取数据
  1.读取文件使用StreamReader类
  2.当使用readline时,文件的末尾用空串表示("")
  让我们从我们刚刚制作的文本文件中读取数据
  now let“s go
   readfile.aspx
   <%@ Import Namespace="System.IO" %>
  <%
  Response.write("Reading the content from the text file ASPNET.TXT <br>")
   “ 声明streamreader对象
  Dim streamreaderobj As StreamReader
   “ 声明filecont变量储存文件中读取的数据
  Dim filecont As String
   “ 打开文本文件并赋于streamreaderobj对象
  streamreaderobj = File.OpenText( "c:\aspnet.txt" )
   “ 读取文件数据直到空值为止
   Do
  filecont = streamreaderobj.ReadLine()
  Response.Write( filecont

TOP

发新话题