C#将日志文件读入listview
我正在尝试导入一个日志文件,并以网格格式(很像excel)在listview中显示它。我想知道什么可能是最好的方法来采取这一点。文件读取器和数据表?我以前没有编写过这样的程序。这是一个windows窗体项目 关于这个问题的任何建议都会大有帮助 EDIT2: 日志文件的示例:C#将日志文件读入listview,c#,winforms,listview,datatable,filereader,C#,Winforms,Listview,Datatable,Filereader,我正在尝试导入一个日志文件,并以网格格式(很像excel)在listview中显示它。我想知道什么可能是最好的方法来采取这一点。文件读取器和数据表?我以前没有编写过这样的程序。这是一个windows窗体项目 关于这个问题的任何建议都会大有帮助 EDIT2: 日志文件的示例: = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = i d = 1 0 0 1 P a r a m e t e r 1 = E
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
i d = 1 0 0 1
P a r a m e t e r 1 = E N A B L E D
P a r a m e t e r 2 = D I S A B L E D
P a r a m e t e r 3 = N U L L
P a r a m e t e r 4 = N U L L
P a r a m e t e r 5 = S U C C E S S
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
这将使用不同的数据重复
我想这是阅读和显示在一个列表视图下的不同标题id,名称等
此应用程序也仅限于使用.NET 3.5。如果您试图在日志文件的每行列表中显示一行,我只需使用
文件。ReadAllLines
读取文件,然后使用字典存储每个日志条目的键值对:
List<Dictionary<string, string>> entries = new List<Dictionary<string, string>>();
Dictionary<string, string> entry = null;
foreach (string line in File.ReadAllLines(logFilePath))
{
string[] fields = line.Split('=');
if (fields.Length > 1)
{
if (fields[0].Trim() == "id")
{
if (entry != null) entries.Add(entry);
entry = new Dictionary<string, string>();
}
if (entry != null) entry[fields[0].Trim()] = fields[1].Trim();
}
}
if (entry != null) entries.Add(entry);
列表条目=新列表();
字典条目=null;
foreach(File.ReadAllLines(logFilePath)中的字符串行)
{
string[]fields=line.Split('=');
如果(fields.Length>1)
{
如果(字段[0].Trim()=“id”)
{
如果(条目!=null)条目。添加(条目);
entry=新字典();
}
如果(entry!=null)条目[fields[0].Trim()]=fields[1].Trim();
}
}
如果(条目!=null)条目。添加(条目);
我的最佳猜测是使用a一次读取一行文件,然后将数据放在a中
编辑:以下代码适用于针对.NET2.0的项目,并假定DataGridView的名称为dataGridView1
StreamReader reader = new StreamReader(@"C:\Users\jdudley\file.txt");
// Will be incremented every time ID shows up so it must started at -1 so we don't
// try and start inserting at 1.
int rowIndex = -1;
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
string[] parsedLine = line.Split(new char[] { '=' });
if(!this.dataGridView1.Columns.Contains(parsedLine[0]))
{
dataGridView1.Columns.Add(parsedLine[0],parsedLine[0]);
}
if (parsedLine[0].Trim().Equals("id"))
{
rowIndex++;
dataGridView1.Rows.Add();
}
dataGridView1[parsedLine[0], rowIndex].Value = parsedLine[1];
}
这是WinForm项目吗?WPF?日志文件的格式是什么?是的。对不起,我更新了questionFilereader和数据表,听起来不错,您的文件是否一致分隔?能否提供文件格式..我再次更新了问题。它是一个.log文件类型这也是我的第一个猜测。不幸的是,我相信DataGridView是.NET4.0。我需要继续使用3.5,我在针对2.0的项目中使用DataGridView,如果是这样的话,我可能读错了东西。有人能澄清一下吗?支持。这是3.5版本的文档,您可以单击当前版本右侧的“其他版本”链接,查看以前版本的文档。谢谢您提供的代码。最后一行给我带来了麻烦。调试后,它似乎会在需要输入时跳过最后一条if语句,因为
parsedLine[]
为空