读取文本文件 [英] textfile reading

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

问题描述

如何在C#中读取文本文件的特定行(例如,文本文件的第三行)?

你能迅速回答我吗?
:confused:

How can I read a specific line of a text file (eg. 3rd line of text file) in C#?

Can you please answer me quickly?
:confused:

推荐答案

请不要要求工作的人免费快速回答".

您可以使用File.ReadAllLines,然后选择所需行的索引.
Please don''t ask the people doing your job for free to ''answer quickly''.

You can use File.ReadAllLines and then choose the index of the line you want.


您好,Jyotshnad,

是的,但是首先您可以阅读全文文件,然后获取特定的行值.

Hi Jyotshnad,

Ya its possible.But first you can read full text file and then to get particular line value.

// Namespace
using System.IO;  

    Private string ReadTextFile(int nReadLine)
    {
        // For Example nReadLine =3
        int nCount=0;
        string sOutput=sting.Empty;         
         using (StreamReader sr = new StreamReader("C:\\SampleFile.txt"))
            {
                string line;                                
                while ((line = sr.ReadLine()) != null)
                {
                   nCount++;
                   if(nCount==nReadLine) 
                     sOutput=line; 
                }
            }        
        return sOutput;
     }




我希望这段代码对您有用.

干杯:)




I hope this code useful to you.

Cheers :)


哦,抱歉

如果您对字符串非常熟悉,并且可以使用流阅读器完成所有操作

选中这个

http://msdn.microsoft.com/en-us/library/8bh11f1k.aspx [ ^ ]
Oh sorry

If you are well familiar with strings and all you can do this by using stream reader

Check this one

http://msdn.microsoft.com/en-us/library/8bh11f1k.aspx[^]


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

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