0

I am developing an Address Book using C#.

i have a list which is displayed in an DataGridView but I want to save the list and not the datagridview into a text file.

When the form loads i have the code:

string[] parts = line.Split(','); // the word line throws an error saying that it does not exist in the current context
Person p = new Person(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], parts[6]);
AddressBook.Persons.Add(p);

I then have this code in the datalayer to save the list:

string filePath = @"c:\test.txt";
p.ToString(); // it does not recognise p

Can anyone help?

4

1 に答える 1

0

ファイルに書き込むためのシンプルで使いやすいメソッドは、File.WriteAllText( filePath, p.ToString()) です。また、クラス Person のメソッド ToString をオーバーライドする必要があります。これにより、ファイルに保存する情報を返すことができます。そうしないと、デフォルトで、クラスの名前が文字列として返されます。最速の方法が必要な場合は、StreamWriter を使用する必要があります。

于 2013-05-26T18:43:44.697 に答える