• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > Windows系统中使用C#读取文本文件内容的小示例

Windows系统中使用C#读取文本文件内容的小示例

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了使用windows7系统,windows7系统使用教程,windows系统使用技巧,windows10系统使用,windows系统使用等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

读取文本文件中的内容

此示例读取文本文件的内容以使用 System.IO.File 选件类的静态方法 ReadAllText 和 ReadAllLines。

class ReadFromFile
{
  static void Main()
  {
    // The files used in this example are created in the topic
    // How to: Write to a Text File. You can change the path and
    // file name to substitute text files of your own.

    // Example #1
    // Read the file as one string.
    string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");

    // Display the file contents to the console. Variable text is a string.
    System.Console.WriteLine("Contents of WriteText.txt = {0}", text);

    // Example #2
    // Read each line of the file into a string array. Each element
    // of the array is one line of the file.
    string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines2.txt");

    // Display the file contents by using a foreach loop.
    System.Console.WriteLine("Contents of WriteLines2.txt = ");
    foreach (string line in lines)
    {
      // Use a tab to indent each line of the file.
      Console.WriteLine("\t" + line);
    }

    // Keep the console window open in debug mode.
    Console.WriteLine("Press any key to exit.");
    System.Console.ReadKey();
  }
}

</div>

一次一行地读取文本文件
本示例使用 StreamReader 类的 ReadLine 方法将文本文件的内容读取(一次读取一行)到字符串中。所有文本行都保存在字符串 line 中并显示在屏幕上。

int counter = 0;
string line;

// Read the file and display it line by line.
System.IO.StreamReader file = 
  new System.IO.StreamReader(@"c:\test.txt");
while((line = file.ReadLine()) != null)
{
  System.Console.WriteLine (line);
  counter++;
}

file.Close();
System.Console.WriteLine("There were {0} lines.", counter);
// Suspend the screen.
System.Console.ReadLine();
</div>

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Windows系统中使用C#编写蓝牙通信程序的简单实例
  • Windows系统中使用C#读取文本文件内容的小示例

相关文章

  • 2017-05-28c#判断正确的ip地址格式示例
  • 2017-05-28C#怎样才能将XML文件导入SQL Server
  • 2017-05-28详解C#中的泛型以及编程中使用泛型的优点
  • 2017-05-28C#基于SQLiteHelper类似SqlHelper类实现存取Sqlite数据库的方法
  • 2017-05-28C#的回调机制浅析
  • 2017-05-28C#常用的数据格式转换汇总
  • 2017-05-28C#实现对文件进行加密解密的方法
  • 2017-05-28C#结合JavaScript实现秒杀倒计时的方法
  • 2017-05-28c#获取本机的IP地址的代码
  • 2017-05-28C#实现屏幕拷贝的方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • c#编写webservice服务引用实例分享
    • c# asp .net 动态创建sql数据库表的方法
    • 无焦点获取条码枪返回值示例
    • C#的回调机制浅析
    • 基于使用递归推算指定位数的斐波那契数列值的解决方法
    • C#实现可缓存网页到本地的反向代理工具实例
    • .Net常见问题之C#中的委托
    • C#可空类型用法分析
    • C#采用OpenXml实现给word文档添加文字
    • C#实现的MD5加密功能与用法示例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有