Windows系统中使用C#读取文本文件内容的小示例
作者:goldensun 时间:2023-05-05 20:27:08
读取文本文件中的内容
此示例读取文本文件的内容以使用 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();
}
}
一次一行地读取文本文件
本示例使用 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();
标签:C#,读取
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
.net从服务器下载文件中文名乱码解决方案
2022-09-11 15:23:11
java 并发线程个数的如何确定
2022-01-01 21:52:13
Springboot中登录后关于cookie和session拦截问题的案例分析
2022-09-25 19:26:44
![](https://img.aspxhome.com/file/2023/2/108932_0s.gif)
C#使用前序遍历、中序遍历和后序遍历打印二叉树的方法
2023-11-24 11:00:37
android 进度条组件ProgressBar
2023-10-20 15:05:33
![](https://img.aspxhome.com/file/2023/0/138360_0s.jpg)
Spring中实现定时调度的几种方法
2021-08-29 13:04:44
Spring Cloud Stream消息驱动组件使用方法介绍
2022-05-30 04:30:52
![](https://img.aspxhome.com/file/2023/2/127222_0s.png)
Activiti流程引擎对象及配置原理解析
2023-02-11 22:20:20
unity实现鼠标跟随(ITween)
2021-09-26 00:01:43
![](https://img.aspxhome.com/file/2023/8/79098_0s.png)
Android中子线程和UI线程通信详解
2023-03-21 00:05:10
SpringBoot定时任务设计之时间轮案例原理详解
2023-04-24 00:48:36
![](https://img.aspxhome.com/file/2023/0/62450_0s.png)
C#基础:Dispose()、Close()、Finalize()的区别详解
2021-10-22 19:20:23
C#用RabbitMQ实现消息订阅与发布
2022-09-05 16:23:40
![](https://img.aspxhome.com/file/2023/6/92876_0s.png)
SpringBoot 应用程序测试实现方案
2021-12-18 04:27:54
![](https://img.aspxhome.com/file/2023/9/86729_0s.jpg)
Android实现绘制LocationMarkerView图的示例代码
2023-01-14 07:55:29
![](https://img.aspxhome.com/file/2023/6/123316_0s.png)
解决微服务中关于用户token处理到的坑
2022-05-21 08:31:03
C#开发Winform控件之打开文件对话框OpenFileDialog类
2023-04-19 10:53:16
![](https://img.aspxhome.com/file/2023/3/116513_0s.jpg)
C#实现文件断点续传下载的方法
2021-09-05 10:37:42
Spring项目中使用Junit单元测试并配置数据源的操作
2022-06-02 05:32:27
Android新浪微博下拉刷新(最新消息显示在最上面)
2023-06-04 00:56:49