【发布时间】:2016-04-05 08:21:28
【问题描述】:
我之前没有创建过自定义类,所以这可能是不可能的,我想读取一些文本文件并存储有关它们的某些信息以在我的程序中使用。
class text
{
public int IDnum { get; set; }
public string file { get; set; }
public int lineNum { get; set; }
public string FileText { get; set; }
public string lineType { get; set; }
}
List<text> listOne = new List<text>();
internal void ReadFile()
{
try
{
int IDtype = 0;
foreach (string x in resultFiles)
{
using (StreamReader sr = new StreamReader(x))
{
string s;//text line
int LINECOUNT = 0;
string type = "not defined";
while ((s = sr.ReadLine()) != null)// this reads the line
{
if(s.Contains("COMPUTERNAME="))
{
type = "PC Name";
}
if (s.Contains("Original Install Date: "))
{
type = "Original Install Date";
}
if (LINECOUNT==2)
{
type = "other Date";
}
if (s.Contains("DisplayName\"="))
{
type = "Add/Remove Programs";
}
text text1 = new text { IDnum = IDtype, lineNum=LINECOUNT, file=x, FileText=s, lineType=type};
LINECOUNT++;
IDtype++;
listOne.Add(text1);
}
sr.Close();
}
}
foreach(var x in listOne)
{
MessageBox.Show(x.ToString());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
但是,当我尝试读取列表时,它只返回相同的值“程序名称。类名称。文本”
我之前从未构建过自定义类,谁能指出我可以学习更多示例的网站?
提前感谢您的任何建议:)
【问题讨论】:
-
在调用
x.ToString()时,您需要覆盖文本类中的.ToString()-Method 以获得正确的结果。调用 ToString() 时你期望得到什么结果?