编写列表项到文本文件c#
本文关键字:文本 文件 列表 | 更新日期: 2023-09-27 18:01:52
我正试图让List
保存到文本文件中,我遇到了一个问题。它将保存到文本文件中,但不是所需的所有信息,而是实际显示在ListBox
中的信息。建议吗?
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();
}
}
}
对不起,我是新手。有两个表单,第二个是用于编辑/添加新员工的。只需要在ListBox中显示姓和名。下面是我的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);
}
}
当您调用SaveFile.WriteLine(item.ToString());
时,您正在编写Employee
的ToString()
方法的结果:
return string.Format("{0}, {1}", LastName, FirstName);
这是ListBox
调用的相同方法,用于在列表中显示对象。所以你所看到的行为正是人们所期望的。
如果你想看到一些不同的东西,试着这样做:
SaveFile.WriteLine(string.Format("{0}, {1}, {2}, {3}", item.LastName, item.FirstName, item.EmpType, item.Salary));