C# IO读取指定行数

1.利用File类,返回带有每一行的string[]数组,强烈推荐使用

public partial Form1 : Form
{
    string[] lines;
    public Form1()
     {
            InitializeComponent();
     }
      
     private void Form1_Load(object sender, System.EventArgs e)
     {
            lines = File.ReadAllLines("TextFile1.txt");
            //第一行在textBox1中显示
            if(lines.Length > 0 )
            {
                textBox1.Text = lines[0];
            }
            //第二行在textBox2中显示
            if(lines.Length > 1)
            {
                textBox2.Text = lines[1];
            }
     }
}

 

 

2. C#2.0前,FileStream,不推荐使用

using System.IO
 
public partial Form1 : Form
{
    List<string> lines;
    public Form1()
     {
            InitializeComponent();
            //存放所有行的集合
            lines = new List<string>();
     }
      
     private void Form1_Load(object sender, System.EventArgs e)
     {
            FileStream fs = new FileStream("TextFile1.txt", FileMode.Open);
            StreamReader rd = new StreamReader(fs);
            string s;
            //读入文件所有行,存放到List<string>集合中
            while( (s= rd.ReadLine() )!= null)
            {
                lines.Add(s);
            }         
            rd.Close();
            fs.Close();
            //第一行在textBox1中显示
            if(lines.Count > 0 )
            {
                textBox1.Text = lines[0];
            }
            //第二行在textBox2中显示
            if(lines.Count > 1)
            {
                textBox2.Text = lines[1];
            }
     }
}

 

转载于:https://www.cnblogs.com/nnnnn/p/4452859.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C#读取文本文件的数,可以使用以下代码: ```csharp using System; using System.IO; class Program { static void Main(string\[\] args) { string filePath = "L:\\Code\\1.txt"; // 文件路径 int lineCount = GetLineCount(filePath); // 调用方法获取数 Console.WriteLine("该文件一共有" + lineCount + ""); Console.ReadKey(); } public static int GetLineCount(string filePath) { int lineCount = 0; using (StreamReader sr = new StreamReader(filePath)) { while (sr.ReadLine() != null) { lineCount++; } } return lineCount; } } ``` 以上代码中,`GetLineCount`方法接收一个文件路径作为参数,使用`StreamReader`读取文件的每一,并通过计数器统计数。最后返回数。在`Main`方法中,我们调用`GetLineCount`方法并输出数。请注意,你需要将文件路径`filePath`替换为你实际的文件路径。\[1\] #### 引用[.reference_title] - *1* [C#读取txt文本内容数](https://blog.csdn.net/qq_22889875/article/details/77924840)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [C#语言读取txt列数据](https://blog.csdn.net/chengoes/article/details/121409682)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [通过程序 VB.Net 或 C# 读取文本文件数](https://blog.csdn.net/weixin_34218890/article/details/86309622)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值