I am trying to read a text file using System.IO.File.ReadAllLines()
and then input each row of the array into a model which I will pass to a view.
I can read the text file successfully but I want to split each row into columns. The text file has fixed-width as the delimiter, and this is where I am failing.
For example I know if comma was the delimiter Project = row.Split(',')[0]
would be correct but how do I handle fixed-width?
My action is below.
I know the first column starts @ character 0, 2nd at 26, 3rd at 82 and 4th at 106
public ActionResult Index()
{
string[] texts = System.IO.File.ReadAllLines(Server.MapPath("~/App_Data/Test/test.txt"));
texts = texts.Skip(2).ToArray();
List<Alarm_DataModel> Alarm_Data = new List<Alarm_DataModel>();
foreach (string row in texts)
{
if (!string.IsNullOrEmpty(row))
{
Alarm_Data.Add(new Alarm_DataModel
{
Project = row.Split('0')[0],
Point = row.Split('26')[1],
TimeStamp = row.Split('82')[2],
DataValue = row.Split('106')[3],
});
}
}
ViewBag.Data = texts;
return View(Alarm_Data);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…