北京彩页设计制作,汕头网站建设方案优化,可以下载的网站模板吗,玛沁县公司网站建设本文从最基本的操作开始#xff0c;解释在ASP.NET中文件处理的概念#xff0c;包括如从一个文件中读取内容、如何向一个文件中写入内容和如何删除一个文件。 前面已经提到#xff0c;要想在ASP.NET 页面中进行文件处理#xff0c;必须要有System.IO名称空间… 本文从最基本的操作开始解释在ASP.NET中文件处理的概念包括如从一个文件中读取内容、如何向一个文件中写入内容和如何删除一个文件。 前面已经提到要想在ASP.NET 页面中进行文件处理必须要有System.IO名称空间。所以第一步就是引入这个名称空间 % Import NamespaceSystem.IO % 下一步就是创建一个文本文件并将这个文本文件分配给一个流书写对象这样就可以向文本文件中写入内容了。用以下一段代码来完成这个任务 writefile.aspx % Import NamespaceSystem.IO % % Response.write(Writing the content into Text File in ASP.NET BR) 声明流书写对象 Dim strwriterobj As StreamWriter 创建文本文件分配textfile对象 strwriterobj File.CreateText(c:aspnet.txt ) 写入内容 strwriterobj.WriteLine( Welcome to wonderfull world of ASP.NET Programming ) 完成操作关闭流对象 strwriterobj.Close Response.write(Done with the creation of text file and writing content into it) % 这样就完成了!现在让我们继续进行下一个任务从刚才创建的文本文件中读取内容。 从文件中读取内容 从文件中读取内容与向文件中写入内容大致相同只是要注意一下下面的两件事 1. 文件读取使用StreamReader类 2. 当使用了Readline方法时将要被读取的文本文件的结尾处会用一个空字符串()来标记。 现在开始编写代码从前面创建的aspnet.txt 文件中读取内容 在ASP.NET中文件处理的整个过程都是围绕着System.IO 这个名称空间展开的。这个名称空间中具有执行文件读、写所需要的类。 readfile.aspx % Import NamespaceSystem.IO % % Response.write(Reading the content from the text file ASPNET.TXT br) 创建流读取对象 Dim streamreaderobj As StreamReader 声明变量以存放从文件中读取的内容 Dim filecont As String 打开文本文件分配给流读取对象 streamreaderobj File.OpenText( c:aspnet.txt ) 逐行读取文件内容 Do filecont streamreaderobj.ReadLine() Response.Write( filecont br ) Loop Until filecont 完成读取操作后关闭流读取对象 streamreaderobj.Close Response.write( brDone with reading the content from the file aspnet.txt) % 删除文件 在ASP.NET中删除文件也非常简单和直观。System.IO名称空间中的File(文件)类有一个Delete方法用来删除文件它把文件名作为一个自变量来传递。以下代码就演示了在ASP.NET中进行文件删除的步骤 Filedelete.aspx % Import NamespaceSystem.IO % % File.Delete(c:aspnet.txt ) Response.write(The File aspnet is deleted successfully !!! ) % 转载于:https://blog.51cto.com/yonghu/1321473