如何使用C#读取文本文件 [英] How to read a text file using C#

查看:67
本文介绍了如何使用C#读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C#读取文本文件



我创建了打开文件对话框并选择了文件。





如何通过关键字

How to read a text file using C#

I have created open file dialog and selected the file.


How to read individual elements by its keyword

推荐答案

读取单个元素



解决方案取决于您要对文本执行的操作。您可以逐行读取文件并从行中提取特定关键字,或者您可以从文件读取整个文本到变量并解析它。

如果文件是大文本,更好的解决方案将被读取但是正如我所说,这取决于你的要求。



阅读文本文件很简单:

Hi,

Solution depends on that what you want to do with text. You can read file line by line and extract specific keyword from line or you can read entire text from file to a variable and them parse it.
If file is large text, better solution will be read line by line, but as i said, it depends on your demands.

Reading text files is simple:
using (var streamReader = new StreamReader(@"C:\file.txt"))
{
    // You can read line by line
    while (!streamReader.EndOfStream)
    {
        var line = streamReader.ReadLine();
        // Do something with readed line
    }
    // OR read entire text and assign to variable
    var allText = streamReader.ReadToEnd();
}





希望它可以帮到你。



Hope it helps you.


使用这个对象,

IO.File [ ^ ]

< a href =http://support.microsoft.com/kb/304430/EN-US> http://support.microsoft.com/kb/304430/EN-US [ ^ ]

http://www.tutorialspoint.com/csharp/csharp_file_io.htm [ ^ ]



-KR
Use this object,
IO.File[^]
http://support.microsoft.com/kb/304430/EN-US[^]
http://www.tutorialspoint.com/csharp/csharp_file_io.htm[^]

-KR


这篇关于如何使用C#读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆