小弟菜菜请教C#文件读写。
小弟写了一个Employee类using System;
namespace Test
{
public class Employee
{
public string empId;
public string empName;
public string empDepartment;
public double empSalary;
}
}
我想使用StreamWriter类写入到Employee.txt文件中
结果如下:
编号 : 001
部门 : sost
姓名 : jack
工资 : 4000
可是为什么我写完的结果却是如下呢?
部门 : sost
姓名 : jack
编号 : 001
工资 : 4000
以下是我写文件的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
namespace Diy
{
public partial class Form1 : Form
{
private StreamWriter sw;
private Hashtable ht = new Hashtable();
private const string path = @"E:\ACCP4.0-S2\C#\第十二章\Diy\Employee.txt";
public static string _empId;
public static string _empName;
public static string _empDepart;
public static double _empSalary;
public Form1()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (File.Exists(path))
{
sw = File.AppendText(path);
}
else
{
sw = File.CreateText(path);
}
//向Hashtable中添加元素
Employee obj = new Employee();
obj.empId = this.txtEmpId.Text;
obj.empName = this.txtEmpName.Text;
obj.empDepart = this.txtEmpDepart.Text;
obj.empSalary = Convert.ToDouble(this.txtEmpSalary.Text);
ht.Add("编号", this.txtEmpId.Text);
ht.Add("姓名", obj.empName);
ht.Add("部门", obj.empDepart);
ht.Add("工资", obj.empSalary);
//写入文件流中
try
{
foreach (DictionaryEntry de in ht)
{
sw.WriteLine(" {0} : {1}", de.Key.ToString(), de.Value.ToString());
}
MessageBox.Show("职员信息录入成功!");
this.txtEmpId.Text = "";
this.txtEmpId.Focus();
this.txtEmpName.Text = "";
this.txtEmpDepart.Text = "";
this.txtEmpSalary.Text = "";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sw.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnSearch_Click(object sender, EventArgs e)
{
frmSearch fs = new frmSearch();
fs.ShowDialog();
this.txtEmpId.Text = _empId;
this.txtEmpName.Text = _empName;
this.txtEmpDepart.Text = _empDepart;
this.txtEmpSalary.Text = _empSalary.ToString();
}
}
}
请高手指正,谢谢了,急用! --------------------编程问答-------------------- Hashtable 是散列表,就是说元素的存储位置是散列的,而不是有序的,也就是说,不是按我们放进去的顺序就行排列的,楼主可以把Hashtable 换成arraylist,如果确实需要 健-值 集合的话就是用 SortedList --------------------编程问答-------------------- 同意 --------------------编程问答-------------------- SortedList 会对健进行排序,可能还是不是你想要的那种结果,所以还是用 ArrayList吧。
--------------------编程问答-------------------- 那应该怎么改呢?我要实现查询的功能,而查询在另一个窗体实现 --------------------编程问答-------------------- 还是次序的问题了 !!!!!!!!!! --------------------编程问答-------------------- HashTable是不可以排序的,可以使用Arraylist实现 --------------------编程问答-------------------- Arraylist --------------------编程问答-------------------- 那就用SortedList 吧,把键值前加个英文字母 编号--A编号 姓名--B姓名
部门--C部门 工资--D工资,
这样在SortedList 的排序就是 A编号,B姓名,C部门,D工资了。
--------------------编程问答-------------------- 用ArrayList就行了. --------------------编程问答-------------------- 根本就没有必要用HashTable
--------------------编程问答-------------------- 用字典吧
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
namespace Diy
{
public partial class Form1 : Form
{
private StreamWriter sw;
private const string path = @"E:\ACCP4.0-S2\C#\第十二章\Diy\Employee.txt";
public static string _empId;
public static string _empName;
public static string _empDepart;
public static double _empSalary;
public Form1()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (File.Exists(path))
{
sw = File.AppendText(path);
}
else
{
sw = File.CreateText(path);
}
//向Hashtable中添加元素
Employee obj = new Employee();
obj.empId = this.txtEmpId.Text;
obj.empName = this.txtEmpName.Text;
obj.empDepart = this.txtEmpDepart.Text;
obj.empSalary = Convert.ToDouble(this.txtEmpSalary.Text);
//写入文件流中
try
{
sw.WriteLine("编号:"+obj.empId);
sw.WriteLine("姓名:"+obj.empName);
sw.WriteLine("部门:"+obj.empDepart);
sw.WriteLine("工资:"+obj.empSalary.ToString());
MessageBox.Show("职员信息录入成功!");
this.txtEmpId.Text = "";
this.txtEmpId.Focus();
this.txtEmpName.Text = "";
this.txtEmpDepart.Text = "";
this.txtEmpSalary.Text = "";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sw.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnSearch_Click(object sender, EventArgs e)
{
frmSearch fs = new frmSearch();
fs.ShowDialog();
this.txtEmpId.Text = _empId;
this.txtEmpName.Text = _empName;
this.txtEmpDepart.Text = _empDepart;
this.txtEmpSalary.Text = _empSalary.ToString();
}
}
}
补充:.NET技术 , C#