易截截图软件、单文件、免安装、纯绿色、仅160KB

ASP.NET创建文件并写入内容

在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")
%>
这样就完成了!现在让我们继续进行下一个任务,从刚才创建的文本文件中读取内容。
从文件中读取内容
从文件中读取内容与向文件中写入内容大致相同,只是要注意一下下面的两件事:
1. 文件读取使用StreamReader类 2. 当使用了Readline方法时,将要被读取的文本文件的结尾处会用一个空字符串("")来标记。
现在开始编写代码从前面创建的aspnet.txt 文件中读取内容:
readfile.aspx
<%@ Import Namespace="System.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("<br>Done with readi


相关文档:

How postback works in ASP.NET

Introduction
In this article, we will take a closer look at how ASP.NET pages post back to themselves, and how to customize this feature in our web applications.
function __doPostBack(eventTarget, eventArgument)
One of the most important features of the ASP.NET environment is the ability to decla ......

ASP.NET 实现多版本語言

public class PageBase:System.Web.UI.Page
{
 public PageBase()
 {
  //
  // TODO: Add constructor logic here
  //
 }
    protected override void InitializeCulture()
    {
        // ......

ASP.NET常用代码

http://www.chinaz.com/Program/.NET/101U1142006.html
  1. 打开新的窗口并传送参数:
传送参数:
response.write("<script>window.open('*.aspx?id="+this.DropDownList1.SelectIndex+"&id1="+...+"')</script>")
接收参数:
string a = Request.QueryString("id");
string b = Request.QuerySt ......

ASP.NET的母版知识摘要


母版页面
-- 新建一母版页面
-- 在适当位置加入代码:
<asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="server">
...这之间的内容如果没有Content内容,将会得到显示;
...如果有Content内容,将会被Content内容替换掉
</asp:ContentPlaceHolder>
Content页面
-- 新建一WEB窗体
-- 在适当 ......

ASP.NET MVC介绍

The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller. The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for creating MVC-based Web applications. The ASP.NET MVC framework is ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号