如何在c#中读取文本文件 [英] How can read text file in c#

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

问题描述

大家好

我有这样的文本文件

 StartTime EndTime级别原因状态
20130120114131 20130120114222 2 03 250
20130120114131 20130120114223 1 05 071
20130120114131 20130120114222 3 06 2695



如何读取此文件和在我的申请中使用?

请帮帮我。

解决方案

那个真的不仅仅是快速查看格式化的文本视图 - 你如何阅读它将取决于实际的文件内容。



你有很多方法可以读取这些数据:如果它是固定列,它们之间有空格,你可以读取文件的每一行并使用string.Substring提取每个数据部分,然后使用各种TryParse和TryParseExact方法将它们转换为适当的数据类型。警惕等级字段 - 你需要知道数字是否这样做:

 2 
3
10

或者这个:

 2 
3
10

开始分割数据之前。



如果它是制表符分隔的,那么你可以使用CSV阅读器,可能使用TryParseExact来分割日期。



使用十六进制读取器查看文件,确切了解您的第一个文件。


您可以尝试以下代码将数据从文本文件移植到datagridview 。您可以根据自己的需要进行调整。另请注意OriginalGriff所说的内容。



我使用您的文本文件作为示例并将其命名为sample.txt并将其放在c驱动器上。 />




 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
使用 System.Diagnostics;

命名空间 WindowsFormsApplication1
{
public partial class Form3:表单
{
public Form3()
{
InitializeComponent();
}

private void Form3_Load( object <> span sender,EventArgs e)
{
DataTable dt = new DataTable();

// 将文本文件的每一行放入字符串数组中。
string [] lines = System.IO.File.ReadAllLines( @ c:\ sample.txt);

// 循环到每一行
for int i = 0 ; i < lines.Length; i ++)
{
DataRow newrow = dt.NewRow();

// 将每行分成空格
< span class =code-keyword> string [] words = lines [i] .Split( new char [] {' '},StringSplitOptions.RemoveEmptyEntries);

// 如果i = 0,则表示这是标题行
if (i == 0
{
for int j = 0 ; j < words.Length; j ++)
{
// 添加列
dt.Columns.Add(words [j]);

}
}
else
{
for int j = 0 ; j < words.Length; j ++)
{
// fill单元格内容
newrow [dt.Columns [j]] = words [j];
}

// 添加新行
dt.Rows.Add(NEWROW);
}
}

dataGridView1.DataSource = dt;
}
}
}


访问这里......



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









用C#读取文本文件 [ ^ ]

Hi all
I have a text file like this

StartTime           EndTime              Level     Reason      Status
20130120114131      20130120114222       2         03          250
20130120114131      20130120114223       1         05          071
20130120114131      20130120114222       3         06          2695


how can I read this file and use in my application ?
please help me .

解决方案

That really depends on more than a quick look at a formatted text view - how you read it will depend on the actual file content.

There are a huge number of ways you could read this data: if it is fixed columns, with spaces between them, the you could read each line of the file and use string.Substring to extract each data section, then use the various TryParse and TryParseExact methods to convert them to teh appropriate datatypes. Be wary though of the "Level" field - you need to know if numbers do this:

 2
 3
10

Or this:

2
 3
 10

before you start splitting data up.

If it is tab separated, then you could use a CSV reader, probably with TryParseExact to split out the dates.

Look at the file using a hex reader as work out exactly what you have first.


You may try out the following code that ports data from text file to a datagridview. It is up to you to adapt it to suit your need. Also take note of what OriginalGriff has said.

I am using your text file as an example and named it as sample.txt and place it on c drive.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();

            // put each line of the text file into a string array. 
            string[] lines = System.IO.File.ReadAllLines(@"c:\sample.txt");

            // loop thru each line
            for (int i = 0; i < lines.Length; i++)
            {
                DataRow newrow = dt.NewRow();

                // split each line into words by spaces
                string[] words = lines[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                // if i = 0, means this is the header row
                if (i == 0)
                {
                    for (int j = 0; j < words.Length; j++)
                    {
                        // add column
                        dt.Columns.Add(words[j]);

                    }
                }
                else
                {
                    for (int j = 0; j < words.Length; j++)
                    {
                        // fill cell content
                        newrow[dt.Columns[j]] = words[j];
                    }

                    // add new row
                    dt.Rows.Add(newrow);
                }
            }

            dataGridView1.DataSource = dt;
        }
    }
}


Visit here...

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

or


Reading a text file in C#[^]


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

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