如何使用streamreader从选定的行读取数据

时间:2017-02-02 05:12:44

标签: c# sql

我有这样的数据

Total Size (Media)..........,517.31 GB
Elapsed Time................,4h 32m 26s
Total Size (Media)..........,517.31 GB
Elapsed Time................,4h 37m 31s
Workstation.................,ITPHOFPDENRP01 ( 10.10.156.20 )
Start Time..................,1/06/17
Workstation.................,ITPHOFPDENRP01 ( 10.10.156.20 )
Path........................,D:\Apps
Total Size (Media)..........,27.00 GB
Elapsed Time................,46m 20s
Total Size (Media)..........,27.00 GB
Elapsed Time................,46m 20s
Workstation.................,ITPBACKUP ( 10.10.160.10 )
Start Time..................,1/06/17 10:04 PM
Workstation.................,ITPBACKUP ( 10.10.160.10 )
Path........................,D:\Job
Total Size (Media)..........,204.38 MB
Elapsed Time................,1m 9s
Total Size (Media)..........,204.38 MB
Elapsed Time................,1m 9s
Workstation.................,ITPBACKUP ( 10.10.160.10 )

如何从行开始时间到下一个开始时间读取数据,并且总是这样,结果必须像这样

Workstation Path Elapsed Time Total Size (Media) Start Time

1 个答案:

答案 0 :(得分:1)

您必须创建一个类来保存此信息。像

这样的东西
 public class RowData
{
    public string Workstation { get; set; }
    public string Path { get; set; }
    public string ElapsedTime { get; set; }
    public string TotalSize { get; set; }
    public string StartTime { get; set; }
}

之后,您阅读文件并遍历行,如下所示

FileStream fs = File.Open(@"D:\Example1.txt", FileMode.Open);

       List<RowData> lstLines = new List<RowData>();
        using (StreamReader stRead = new StreamReader(fs))
        {
            RowData oRowData = null;
            while (!stRead.EndOfStream)
            {
                string sLine = stRead.ReadLine();
                if (sLine.IndexOf("Start Time") > -1)
                {
                    if (oRowData != null)
                    {
                        lstLines.Add(oRowData);
                        oRowData = null;
                    }
                    oRowData = new RowData();
                    sLine = sLine.Replace(".", "");
                    sLine = sLine.Replace("Start Time", "");
                    oRowData.StartTime = sLine;    
                }
                if (sLine.IndexOf("Workstation") > -1)
                {
                    if (oRowData != null)
                    {
                        sLine = sLine.Replace(".", "");
                        sLine = sLine.Replace("Workstation", "");
                        oRowData.Workstation = sLine;
                    }
                }
            }
        }

填写完RowData列表后,您可以按照自己的方式使用它......

 foreach (var item in lstLines)
        {
            Console.WriteLine(item.StartTime + "|" + item.Workstation);
        }

请注意,您必须删除这些&#34;。&#34;字符