我之前沒有創建自定義類,所以這可能是不可能的,我想讀取一些文本文件並存儲有關它們的特定信息,以便在我的程序中使用它們。在c中構建一個自定義類#
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());
}
}
然而,當我嘗試閱讀列表,它只是返回相同的值「class.text的program.name的名字」
我以前從來沒有內置自定義類任何人都可以點我到一個網站我可以在哪裏學到更多的例子?
預先感謝任何建議:)
您需要重寫文本類中的'.ToString()'-Method以在調用'x.ToString()'時獲得正確的結果。調用ToString()時你期望得到什麼結果? – Camo