小弟菜菜请教C#文件读写。 Hashtable 是散列表,就是说元素的存储位置是散列的,而不是有序的,也就是说,不是按我们放进去的顺序就行排列的,楼主可以把Hashtable 换成arraylist,如果确实需要 健-值 集合的话就是用 SortedList 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 SortedList 会对健进行排序,可能还是不是你想要的那种结果,所以还是用 ArrayList吧。 HashTable是不可以排序的,可以使用Arraylist实现 那就用SortedList 吧,把键值前加个英文字母 编号--A编号 姓名--B姓名部门--C部门 工资--D工资,这样在SortedList 的排序就是 A编号,B姓名,C部门,D工资了。 根本就没有必要用HashTableusing 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(); } }} wpf 应用程序如何获得当前根目录并且判断根目录下是否存在某一文件 微软报表rdlc DataTable 里 查找重复的行 c#中如何单元测试? frameset如何直接显示Excel页面而不显示对话框 vs.net无法进入,请求教 有没有 VB.NET代码转换为C#代码 的工具 十万火急~~求教~!求教~ 关于wcf中的custombinding 求在工作期间进步最快的方法 大侠们帮帮忙!!GDI+ 各位高手大家好,我工作中碰到一个需要将Jmail附件转换成流或字节数组的问题,希望各位能指点我一下,谢谢大家了!
部门--C部门 工资--D工资,
这样在SortedList 的排序就是 A编号,B姓名,C部门,D工资了。
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();
}
}
}