C#操作文本文件实例浅析

开发 后端
C#操作文本文件实例向你讲述了C#操作文本文件的具体操作的过程,希望对你了解C#操作文本文件有所帮助。

C#操作文本文件是如何实现的呢?让我们开始讲述吧:

  1. using System.IO;  
  2.  
  3. //声明控件  
  4. protected System.Web.UI.HtmlControls.HtmlTextArea txtValue; 

C#操作文本文件之读取文本文件:

  1. //主程序  
  2. FileStream   fsInfo = new  FileStream(   
  3. "文件路径(在项目内的)", FileMode.Open, FileAccess.Read );  
  4. StreamReader  srInfo  =  new StreamReader(   
  5. fsInfo, System.Text. Encoding.GetEncoding( "GB2312" ) );  
  6. srInfo.BaseStream.Seek( 0, SeekOrigin.Begin );  
  7. txtValue.Value  = " ";  
  8. string strLine  = srInfo.ReadInfo();  
  9. while( strLine != null )  
  10. {  
  11. txtValue.Value  +=   strLine  + "\n";  
  12. strLine   =   srInfo.ReadLine();  
  13. }  
  14. srInfo.Close(); 

C#操作文本文件之写入文本文件:

  1. //主程序  
  2. FileStream fsInfo  = new FileStream(  
  3.  文件路径(在项目内的)", FileMode.OpenOrCreate, FileAccess.Write );  
  4. StreamWriter swInfo = new StreamWriter( fsInfo );  
  5. swInfo.Flush();  
  6. swInfo.BaseStream.Seek( 0, SeekOrigin.Begin );  
  7. swInfo.Write( txtValue.Value );  
  8.  
  9. swInfo.Flush();  
  10. swInfo.Close(); 

C#操作文本文件的基本实现内容就向你介绍到这里,希望对你了解和学习C#操作文本文件有所帮助。

【编辑推荐】

  1. C# 操作符基础知识总结
  2. C# 操作符之is应用浅析
  3. C# 操作符之三元操作符浅析
  4. C# 操作符之 . 运算符应用详解
  5. C# 操作符分类及应用浅析
责任编辑:仲衡 来源: bianceng.cn
相关推荐

2009-08-20 10:17:27

C#操作文本文件

2009-08-20 09:15:20

C#操作文本文件

2009-08-20 09:58:06

C#操作文本文件

2009-09-02 19:13:08

C#处理文本文件

2009-09-02 19:08:03

C#实现读取文本文件

2009-08-26 11:53:56

C#打印文本文件

2009-08-20 09:26:14

C#操作文本文件

2009-09-04 15:56:35

写入文本文件

2009-08-06 18:33:45

C#处理文本文件

2010-01-11 17:05:32

VB.NET操作文本文

2009-08-12 17:59:48

C#读取文本文

2009-08-31 18:38:59

C#写文件

2009-08-18 13:49:21

C# 操作Excel

2009-08-12 17:42:57

C#读文本文件

2010-02-01 14:26:50

C++读写文本文件

2010-04-30 17:38:31

Unix文本

2009-08-18 16:04:12

C# 操作Excel

2009-08-19 09:42:52

C#操作Word书签

2021-11-29 09:46:11

FileReaderJava开发

2009-08-19 11:34:06

C#操作Word
点赞
收藏

51CTO技术栈公众号