<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JohnZon 世界 &#187; 创建</title>
	<atom:link href="http://www.johnzon.cn/tag/create/feed" rel="self" type="application/rss+xml" />
	<link>http://www.johnzon.cn</link>
	<description>计算机技术 &#124; 生活随笔</description>
	<lastBuildDate>Wed, 23 Jun 2010 08:45:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ASP.NET创建文件并写入内容</title>
		<link>http://www.johnzon.cn/aspnet-establish-write.html</link>
		<comments>http://www.johnzon.cn/aspnet-establish-write.html#comments</comments>
		<pubDate>Sun, 25 May 2008 03:01:47 +0000</pubDate>
		<dc:creator>johnzon</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[写入]]></category>
		<category><![CDATA[创建]]></category>

		<guid isPermaLink="false">http://www.johnzon.cn/archives/21534</guid>
		<description><![CDATA[本文从最基本的操作开始，解释在ASP.NET中文件处理的概念，包括如从一个文件中读取内容、如何向一个文件中写入内容和如何删除一个文件。
前面已经提到，要想在ASP.NET 页面中进行文件处理，必须要有&#8221;System.IO&#8221;名称空间。所以，第一步就是引入这个名称空间：
%@ Import Namespace=&#8221;System.IO&#8221; %
下一步，就是创建一个文本文件，并将这个文本文件分配给一个流书写对象，这样就可以向文本文件中写入内容了。用以下一段代码来完成这个任务：
writefile.aspx
%@ Import Namespace=&#8221;System.IO&#8221; %
%
Response.write(&#8220;Writing the content into Text File in ASP.NET BR&#8221;)
&#8220;声明流书写对象
Dim strwriterobj As StreamWriter
&#8221; 创建文本文件，分配textfile对象
strwriterobj= File.CreateText(&#8220;c:aspnet.txt&#8221; )
&#8221; 写入内容
strwriterobj.WriteLine( &#8220;Welcome to wonderfull world of ASP.NET Programming&#8221; ) &#8221;
完成操作，关闭流对象
strwriterobj.Close
Response.write(&#8220;Done with the creation of text file and writing content into it&#8221;)
%
这样就完成了!现在让我们继续进行下一个任务，从刚才创建的文本文件中读取内容。
从文件中读取内容
从文件中读取内容与向文件中写入内容大致相同，只是要注意一下下面的两件事：
1. 文件读取使用StreamReader类
2. 当使用了Readline方法时，将要被读取的文本文件的结尾处会用一个空字符串(&#8220;&#8221;)来标记。
现在开始编写代码从前面创建的aspnet.txt 文件中读取内容：
在ASP.NET中，文件处理的整个过程都是围绕着System.IO 这个名称空间展开的。这个名称空间中具有执行文件读、写所需要的类。
readfile.aspx
%@ Import Namespace=&#8221;System.IO&#8221; %
%
Response.write(&#8220;Reading the content from the text [...]]]></description>
			<content:encoded><![CDATA[<p>本文从最基本的操作开始，解释在ASP.NET中文件处理的概念，包括如从一个文件中读取内容、如何向一个文件中写入内容和如何删除一个文件。</p>
<p>前面已经提到，要想在ASP.NET 页面中进行文件处理，必须要有&#8221;System.IO&#8221;名称空间。所以，第一步就是引入这个名称空间：</p>
<p>%@ Import Namespace=&#8221;System.IO&#8221; %</p>
<p>下一步，就是创建一个文本文件，并将这个文本文件分配给一个流书写对象，这样就可以向文本文件中写入内容了。用以下一段代码来完成这个任务：</p>
<p>writefile.aspx</p>
<p>%@ Import Namespace=&#8221;System.IO&#8221; %<br />
%</p>
<p>Response.write(&#8220;Writing the content into Text File in ASP.NET BR&#8221;)</p>
<p>&#8220;声明流书写对象</p>
<p>Dim strwriterobj As StreamWriter</p>
<p><span id="more-58"></span>&#8221; 创建文本文件，分配textfile对象</p>
<p>strwriterobj= File.CreateText(&#8220;c:aspnet.txt&#8221; )</p>
<p>&#8221; 写入内容</p>
<p>strwriterobj.WriteLine( &#8220;Welcome to wonderfull world of ASP.NET Programming&#8221; ) &#8221;</p>
<p>完成操作，关闭流对象</p>
<p>strwriterobj.Close</p>
<p>Response.write(&#8220;Done with the creation of text file and writing content into it&#8221;)</p>
<p>%</p>
<p>这样就完成了!现在让我们继续进行下一个任务，从刚才创建的文本文件中读取内容。</p>
<p>从文件中读取内容</p>
<p>从文件中读取内容与向文件中写入内容大致相同，只是要注意一下下面的两件事：</p>
<p>1. 文件读取使用StreamReader类</p>
<p>2. 当使用了Readline方法时，将要被读取的文本文件的结尾处会用一个空字符串(&#8220;&#8221;)来标记。</p>
<p>现在开始编写代码从前面创建的aspnet.txt 文件中读取内容：</p>
<p>在ASP.NET中，文件处理的整个过程都是围绕着System.IO 这个名称空间展开的。这个名称空间中具有执行文件读、写所需要的类。</p>
<p>readfile.aspx</p>
<p>%@ Import Namespace=&#8221;System.IO&#8221; %<br />
%</p>
<p>Response.write(&#8220;Reading the content from the text file ASPNET.TXT br&#8221;)</p>
<p>&#8221; 创建流读取对象</p>
<p>Dim streamreaderobj As StreamReader</p>
<p>&#8221; 声明变量，以存放从文件中读取的内容</p>
<p>Dim filecont As String</p>
<p>&#8221; 打开文本文件，分配给流读取对象</p>
<p>streamreaderobj = File.OpenText( &#8220;c:aspnet.txt&#8221; )</p>
<p>&#8221; 逐行读取文件内容</p>
<p>Do</p>
<p>filecont = streamreaderobj.ReadLine()</p>
<p>Response.Write( filecont &amp; &#8221; br&#8221; )</p>
<p>Loop Until filecont = &#8220;&#8221;</p>
<p>&#8221; 完成读取操作后，关闭流读取对象</p>
<p>streamreaderobj.Close</p>
<p>Response.write(&#8221; br Done with reading the content from the file aspnet.txt&#8221;)</p>
<p>%</p>
<p>删除文件</p>
<p>在ASP.NET中删除文件也非常简单和直观。System.IO名称空间中的&#8221;File&#8221;(文件)类有一个Delete方法用来删除文件，它把文件名作为一个自变量来传递。以下代码就演示了在ASP.NET中进行文件删除的步骤：</p>
<p>Filedelete.aspx</p>
<p>%@ Import Namespace=&#8221;System.IO&#8221;<br />
%</p>
<p>File.Delete(&#8220;c:aspnet.txt&#8221; )</p>
<p>Response.write(&#8220;The File aspnet is deleted successfully !!!&#8221; )</p>
<p>%</p>
<hr /><p><small>Copyright © 2007-2009 <a href='http://www.johnzon.cn/'>JohnZon 世界</a></small></p><ul class="related_post"><li><a href="http://www.johnzon.cn/analysis-aspnet-ajax-object-thought.html" title="剖析ASP.NET AJAX的面向对象思想">剖析ASP.NET AJAX的面向对象思想</a></li><li><a href="http://www.johnzon.cn/c-sharp-rule.html" title="解读C＃中的规则表达式">解读C＃中的规则表达式</a></li><li><a href="http://www.johnzon.cn/aspnet-new-characteristic.html" title="ASP.NET新特性系列讲座">ASP.NET新特性系列讲座</a></li><li><a href="http://www.johnzon.cn/jsp-aspnet-session.html" title="JSP与ASP.NET之间的session值共享">JSP与ASP.NET之间的session值共享</a></li><li><a href="http://www.johnzon.cn/aspnet-connect-sqlserver.html" title="ASP.NET2.0连接SQL Server数据库详解">ASP.NET2.0连接SQL Server数据库详解</a></li><li><a href="http://www.johnzon.cn/donet-login-kongjian.html" title="第一次用.net2.0 LOGIN登陆控件的困惑和解决方法">第一次用.net2.0 LOGIN登陆控件的困惑和解决方法</a></li><li><a href="http://www.johnzon.cn/aspnet-web-page-yingyong.html" title="ASP.NET Web Page应用深入探讨">ASP.NET Web Page应用深入探讨</a></li><li><a href="http://www.johnzon.cn/ajax-lectures-download.html" title="asp.net ajax视频教程下载">asp.net ajax视频教程下载</a></li><li><a href="http://www.johnzon.cn/regularexpressionvalidator-rule.html" title="RegularExpressionValidator控件中正则表达式用法">RegularExpressionValidator控件中正则表达式用法</a></li><li><a href="http://www.johnzon.cn/aptech-aspnet-download.html" title="北大青鸟ASP.NET视频教程32讲下载">北大青鸟ASP.NET视频教程32讲下载</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.johnzon.cn/aspnet-establish-write.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
