在ASP.NET中,文件处理的整个过程都是围绕着System.IO 这个名称空间展开的。这个名称空间中具有执行文件读、写所需要的类。本文从最基本的操作开始,解释在ASP.NET中文件处理的概念,包括如从一个文件中读取内容、如何向一个文件中写入内容和如何删除一个文件。 前面已经提到,要想在ASP.NET 页面中进行文件处理,必须要有"System.IO"名称空间。所以,第一步就是引入这个名称空间: <%@ Import Namespace="System.IO" %> 下一步,就是创建一个文本文件,并将这个文本文件分配给一个流书写对象,这样就可以向文本文件中写入内容了。用以下一段代码来完成这个任务: writefile.aspx <%@ Import Namespace="System.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") %>
我想把我自己的streamwriter保存成文件,而不是重新生成一个streamwriter
前面已经提到,要想在ASP.NET 页面中进行文件处理,必须要有"System.IO"名称空间。所以,第一步就是引入这个名称空间:
<%@ Import Namespace="System.IO" %>
下一步,就是创建一个文本文件,并将这个文本文件分配给一个流书写对象,这样就可以向文本文件中写入内容了。用以下一段代码来完成这个任务:
writefile.aspx
<%@ Import Namespace="System.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")
%>
strwriterobj.Close();
执行strwriterobj.Close();时 数据就写进c:aspnet.txt 文件
问一下 你的streamwriter不是通过File.CreateText创建的吗
想通过Page.Response.OutputStream ,不知道可不可以办到呢?