[英]Writing List items to text file c#
I am trying to get a List
to save into a text file and am running into a problem. 我正在尝试获取要保存到文本文件的List
,并且遇到了问题。 It will save into the text file but not all the information as needed but only the information that actually shows in the ListBox
. 它将保存到文本文件中,但不是所有需要的信息,而是仅保存在ListBox
中实际显示的信息。 Suggestions? 建议?
namespace Employee_Form
{
public partial class frmMain : Form
{
FileStream output;
StreamReader fileReader;
//StreamWriter fileWriter;
List<Employee> employeeList = new List<Employee>();
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFile();
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveAs();
}
private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
{
PropertiesOpen();
}
private void PropertiesOpen()
{
//creates an instance of the Properties Form
frmProperties myform = new frmProperties();
DialogResult result = myform.ShowDialog();
}
//Opens a file chosen by a user and places information into the listbox
private void OpenFile()
{
OpenFileDialog fileChooser = new OpenFileDialog();
fileChooser.Title = "Pick a file";
fileChooser.Filter = "Text Files (*.txt) | *.txt";
DialogResult result = fileChooser.ShowDialog();
//
if (result == DialogResult.Cancel)
{
//do nothing
return;
}
string strFileName = fileChooser.FileName;
try
{
//open the file for read access
output = new FileStream(strFileName, FileMode.Open, FileAccess.Read);
fileReader = new StreamReader(output);
//variables to hold read record
string strInputLine;
string[] fields;
//loop to get records and break into fields
while (fileReader.EndOfStream != true)
{
//read record
strInputLine = fileReader.ReadLine();
//split the records when read
fields = strInputLine.Split(',');
//add records to the list box
employeeList.Add(new Employee(fields[1], fields[0], fields[2],
Convert.ToDouble(fields[3])));
}
lstRecords.DataSource = employeeList;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//closes fileReader and output to save Resources
fileReader.Close();
output.Close();
}
}
public void SaveAs()
{
//create a file dialog
SaveFileDialog fileChooser = new SaveFileDialog();
fileChooser.Title = "Choose A Save Location";
fileChooser.Filter = "Text Files (*txt)|*.txt";
//open the dialog and get a result
DialogResult result = fileChooser.ShowDialog();
//checks if user clicks cancel
if (result == DialogResult.Cancel)
{
return;
}
//get the file name from the dialog
string strFileName = fileChooser.FileName;
try
{
//open the new file for write access
StreamWriter SaveFile = new StreamWriter(strFileName);
foreach (var item in employeeList)
{
SaveFile.WriteLine(item.ToString());
}
SaveFile.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//close resources
//fileWriter.Close();
output.Close();
}
}
}
Sorry, I am new to this. 抱歉,我是新来的。 There are two forms and the second one is for editing/adding new employees. 有两种形式,第二种形式是用于编辑/添加新员工。 only need to show the first and last name in the ListBox. 只需要在ListBox中显示名字和姓氏即可。 Here is my Employee class also: 这也是我的Employee类:
public class Employee
{
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
public string EmpType
{
get;
set;
}
public double Salary
{
get;
set;
}
public Employee(string firstName, string lastName, string empType, double salary)
{
FirstName = firstName;
LastName = lastName;
EmpType = empType;
Salary = salary;
}
public override string ToString()
{
return string.Format("{0}, {1}", LastName, FirstName);
}
}
When you call SaveFile.WriteLine(item.ToString());
当您调用SaveFile.WriteLine(item.ToString());
, you're writing the result of the ToString()
method of Employee
: ,您正在编写Employee
的ToString()
方法的结果:
return string.Format("{0}, {1}", LastName, FirstName);
This is the same method called by the ListBox
to display the object in the list. 这是ListBox
调用以在ListBox
中显示对象的相同方法。 So the behavior you're seeing is exactly what one would expect. 因此,您所看到的行为正是人们所期望的。
If you want to see something different, try something like this: 如果您希望看到不同的内容,请尝试以下操作:
SaveFile.WriteLine(string.Format("{0}, {1}, {2}, {3}", item.LastName, item.FirstName, item.EmpType, item.Salary));
Use whatever properties and formatting you want in your file. 使用文件中所需的任何属性和格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.