/// <summary>
/// 添加
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
BinaryWriter binWriter = new BinaryWriter(File.OpenWrite(datfile), Encoding.Unicode);//StreamWriter
binWriter.Seek(0, SeekOrigin.End);
num =int.Parse(this.textBox1.Text.Trim());
strsex = this.textBox2.Text.Trim();
strname = this.textBox3.Text.Trim();
creatdate =this.dateTimePicker1.Value;
binWriter.Write(num);
binWriter.Write(strsex);
binWriter.Write(strname);
binWriter.Write(creatdate.ToShortDateString() + Environment.NewLine);
binWriter.Flush();
binWriter.Close();
}
/// <summary>
/// 顯示全部
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
//創建文件
//Write out to binary file
//if (!File.Exists(datfile))
//{
// FileStream myFile = File.Create(datfile);
// myFile.Close();
//}
//strsex = "男";
//num = 124;
//creatdate = DateTime.Now;
//strname = "塗聚文geovindu";
//BinaryWriter binWriter = new BinaryWriter(File.OpenWrite(datfile), Encoding.Unicode);//StreamWriter
//binWriter.Write(num);
//binWriter.Write(strsex);
//binWriter.Write(strname);
//binWriter.Write(creatdate.ToShortDateString() + Environment.NewLine);
binWriter.Seek(1, SeekOrigin.Begin);
//strsex = "男";
//num = 125;
//creatdate = DateTime.Now;
//strname = "劉志軍";
BinaryWriter binWriter = new BinaryWriter(File.OpenWrite(datfile));
//binWriter.Write(num);
//binWriter.Write(strsex);
//binWriter.Write(strname);
//binWriter.Write(creatdate.ToShortDateString() + Environment.NewLine);
binWriter.Seek(2, SeekOrigin.Current);
//strsex = "女";
//num = 126;
//creatdate = DateTime.Now;
//strname = "孩子";
BinaryWriter binWriter = new BinaryWriter(File.OpenWrite(datfile));
//binWriter.Write(num);
//binWriter.Write(strsex);
//binWriter.Write(strname);
//binWriter.Write(creatdate.ToShortDateString() + Environment.NewLine);
binWriter.Seek(3, SeekOrigin.Current);
//binWriter.Flush();
//binWriter.Close();
//Read from binary file
//最一條
//byte[] bs;
//using (FileStream fs = new FileStream(datfile, FileMode.Open, FileAccess.Read))
//{
// using (BinaryReader br = new BinaryReader(fs))
// {
// bs = br.ReadBytes((int)fs.Length);
// }
//}
//foreach (byte detail in bs)
//{
// Console.WriteLine(detail);
//}
dt = null;
dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Sex", typeof(string));
dt.Columns.Add("ReName", typeof(string));
dt.Columns.Add("CreateTime", typeof(DateTime));
using (BinaryReader binaryReader = new BinaryReader(File.Open(datfile, FileMode.Open), Encoding.Unicode))
{
//binaryReader.BaseStream.Seek(0, SeekOrigin.Begin);
//1
//while (true)
//{
// num = binaryReader.ReadInt32();
// strsex = binaryReader.ReadString();
// strname = binaryReader.ReadString();
// creatdate = DateTime.Parse(binaryReader.ReadString());
// //data.Add(k, v);
// if (binaryReader.BaseStream.Position == binaryReader.BaseStream.Length)
// break;
//}
//
//2
while (binaryReader.PeekChar() != -1)
{
try
{
num = binaryReader.ReadInt32();
strsex = binaryReader.ReadString();
strname = binaryReader.ReadString();
creatdate = DateTime.Parse(binaryReader.ReadString());
dt.Rows.Add(num, strsex, strname, creatdate);
}
catch (Exception ex)
{
ex.Message.ToString();
break;
}
}
binaryReader.Close();
}
//BinaryReader binReader = new BinaryReader(File.OpenRead(datfile), Encoding.Unicode);
//num = binReader.ReadInt32();
//strsex = binReader.ReadString();
//strname = binReader.ReadString();
//creatdate = DateTime.Parse(binReader.ReadString());
this.textBox1.Text = num.ToString();
this.textBox2.Text = strsex;
this.textBox3.Text = strname;
this.dateTimePicker1.Value = creatdate;
//binReader.PeekChar();
//num = binReader.ReadInt32();
//this.textBox4.Text = num.ToString();
//binReader.Close();
this.dataGridView1.DataSource = dt;
}
/// <summary>
///
/// </summary>
/// <param name="FilePath"></param>
/// <param name="buff"></param>
public static void WriteBuffer(string FilePath, byte[] buff)
{
StreamWriter Writer = File.AppendText(FilePath);
foreach (byte i in buff)
Writer.BaseStream.WriteByte(i);
Writer.Close();
}
/// <summary>
/// 查找
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
string s = this.textBox4.Text.Trim();
using (BinaryReader binaryReader = new BinaryReader(File.Open(datfile, FileMode.Open), Encoding.Unicode))
{
//binaryReader.BaseStream.Seek(0, SeekOrigin.Begin);
//1
//while (true)
//{
// num = binaryReader.ReadInt32();
// strsex = binaryReader.ReadString();
// strname = binaryReader.ReadString();
// creatdate = DateTime.Parse(binaryReader.ReadString());
// //data.Add(k, v);
// if (binaryReader.BaseStream.Position == binaryReader.BaseStream.Length)
// break;
//}
//
//2
while (binaryReader.PeekChar() != -1)
{
try
{
num = binaryReader.ReadInt32();
strsex = binaryReader.ReadString();
strname = binaryReader.ReadString();
creatdate = DateTime.Parse(binaryReader.ReadString());
if (strname.Contains(s))
{
this.label2.Text=binaryReader.BaseStream.Position.ToString();
this.label3.Text =(binaryReader.BaseStream.Length-binaryReader.BaseStream.Seek(0, SeekOrigin.Current)).ToString();
setCurrent = binaryReader.BaseStream.Seek(0, SeekOrigin.Current);
//binaryReader.BaseStream.Seek((binaryReader.BaseStream.Position / 50) * 50, 0);
this.textBox1.Text = num.ToString();
this.textBox2.Text = strsex;
this.textBox3.Text = strname;
this.dateTimePicker1.Value = creatdate;
}
}
catch (Exception ex)
{
ex.Message.ToString();
break;
}
}
binaryReader.Close();
}
}
/// <summary>
/// 修改,創建
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
#region 創建 塗聚文 20130329
//if (File.Exists(datfile))
//{
// File.Delete(datfile);
//}
//dt = null;
//dt = new DataTable();
//dt.Columns.Add("ID", typeof(int));
//dt.Columns.Add("Sex", typeof(string));
//dt.Columns.Add("ReName", typeof(string));
//dt.Columns.Add("CreateTime", typeof(DateTime));
//dt.Rows.Add(1, "男", "塗聚文", "2013-03-1");
//dt.Rows.Add(2, "女", "王熙鳳", "2013-03-11");
//dt.Rows.Add(3, "女", "劉麗", "2013-03-21");
//dt.Rows.Add(4, "男", "劉海粟", "2013-03-3");
//dt.Rows.Add(5, "男", "高春輝", "2013-03-5");
//BinaryWriter binWriter = new BinaryWriter(File.OpenWrite(datfile), Encoding.Unicode);//StreamWriter
binWriter.BaseStream.Seek(0, SeekOrigin.Current);
//binWriter.Seek((int)setCurrent, SeekOrigin.Current);//定位有問題
//foreach (DataRow dr in dt.Rows)
//{
// num=(int)dr["ID"];
// strsex =(string) dr["Sex"];
// strname = (string)dr["ReName"];
// creatdate =(DateTime)dr["CreateTime"];
// binWriter.Write(num);
// binWriter.Write(strsex);
// binWriter.Write(strname);
// binWriter.Write(creatdate.ToShortDateString() + Environment.NewLine); //+ Environment.NewLine //加一行,成了現在的行
//}
//binWriter.Flush();
//binWriter.Close();
#endregion
//num = int.Parse(this.textBox1.Text.Trim());
//strsex = this.textBox2.Text.Trim();
//strname = this.textBox3.Text.Trim();
//creatdate = this.dateTimePicker1.Value;
//binWriter.Write(num);
//binWriter.Write(strsex);
//binWriter.Write(strname);
//binWriter.Write(creatdate.ToShortDateString()); //+ Environment.NewLine //加一行,成了現在的行
//binWriter.Flush();
//binWriter.Close();
//using (BinaryReader binaryReader = new BinaryReader(File.Open(datfile, FileMode.Open), Encoding.Unicode))
//{
// while (binaryReader.PeekChar() != -1)
// {
// //binaryReader.BaseStream.Seek(setCurrent, SeekOrigin.Begin);
// num = binaryReader.ReadInt32();
// strsex = binaryReader.ReadString();
// strname = binaryReader.ReadString();
// creatdate = DateTime.Parse(binaryReader.ReadString());
// this.textBox1.Text = num.ToString();
// this.textBox2.Text = strsex;
// this.textBox3.Text = strname;
// this.dateTimePicker1.Value = creatdate;
// }
//}
#region 修改
BinaryWriter binWriter = new BinaryWriter(File.OpenWrite(datfile), Encoding.Unicode);//StreamWriter
binWriter.BaseStream.Position = 0;
num = 8;// int.Parse(this.textBox1.Text.Trim());
strsex = "女";// this.textBox2.Text.Trim();
//strname = this.textBox3.Text.Trim();
//creatdate = this.dateTimePicker1.Value;
binWriter.Write(num);
binWriter.BaseStream.Position = 4;
binWriter.Write(strsex);
//binWriter.Write(strname);
//binWriter.Write(creatdate.ToShortDateString()); //+ Environment.NewLine //加一行,成了現在的行
binWriter.Flush();
binWriter.Close();
#endregion
#region 查找數據位置
cudt = null;
cudt = new DataTable();
cudt.Columns.Add("ID", typeof(int));
cudt.Columns.Add("Position", typeof(int));
cudt.Columns.Add("Legnth", typeof(int));
cudt.Columns.Add("Sart", typeof(int));
cudt.Columns.Add("End", typeof(int));
cudt.Columns.Add("Name", typeof(string));
cudt.Columns.Add("Value", typeof(string));
int pos = 0;
int row = 0;
int p = 0;
int br = 0;
string s = "";
int starpos = 0;
int endpos = 0;
// 1.
using (BinaryReader b = new BinaryReader(File.Open(datfile, FileMode.Open, FileAccess.Read),Encoding.Unicode))
{
// 2.
// Position and length variables.
// 2A.
// Use BaseStream.
int length = (int)b.BaseStream.Length;
int k = 0;
while (b.PeekChar() != -1)//pos < length
{
try
{
starpos = (int)b.BaseStream.Position;
//
num = b.ReadInt32();
p = (int)b.BaseStream.Position;
endpos = (int)b.BaseStream.Position;
pos = p;
// b.BaseStream.Seek(pos, SeekOrigin.Current);
br = (int)b.BaseStream.Length-p;
s = b.GetType().ToString();
cudt.Rows.Add(row, pos, br, starpos, endpos, s,num.ToString());//添加第一記錄
//第二條記錄
starpos = (int)b.BaseStream.Position;
strsex = b.ReadString();
p = (int)b.BaseStream.Position;
endpos = p;
pos = p;
br = (int)b.BaseStream.Length-p;
s = b.GetType().ToString();
cudt.Rows.Add(row, pos, br, starpos, endpos, s,strsex);//添加第二條記錄
//第三第記錄
starpos=(int)b.BaseStream.Position;
strname = b.ReadString();
p = (int)b.BaseStream.Position;
endpos = p;
pos = p;
// b.BaseStream.Seek(pos, SeekOrigin.Current);
br = (int)b.BaseStream.Length - p;
s = b.GetType().ToString();
cudt.Rows.Add(row, p, br, starpos, endpos, s,strname);//添加第三條記錄
//第四條記錄
starpos = (int)b.BaseStream.Position;
creatdate = DateTime.Parse(b.ReadString());
p = (int)b.BaseStream.Position;
endpos = p;
pos = p;
br = (int)b.BaseStream.Length - p;
s = b.GetType().ToString();
cudt.Rows.Add(row, p, br, starpos, endpos, s, creatdate);//第四記錄
k++;
}
catch (Exception ex)
{
ex.Message.ToString();
break;
}
// 4.
// Advance our position variable.
pos += sizeof(int);
row++;
}
//b.BaseStream.Seek(7, SeekOrigin.Current);
//string f=b.ReadString();
//MessageBox.Show(f);
//指定位顯示
b.BaseStream.Position = 0;
MessageBox.Show(b.ReadInt32().ToString());
b.BaseStream.Position = 4;
MessageBox.Show(b.ReadString());
b.BaseStream.Position = 7;
MessageBox.Show(b.ReadString());
b.Close();
}
this.dataGridView2.DataSource = cudt;
#endregion
}
/// <summary>
///
/// </summary>
/// <param name="sectorid"></param>
/// <returns></returns>
//public string getclientid(string sectorid)
//{
// try
// {
// DataSet dsobj = new DataSet();
// bllobj = new ConverterClass();
// string retclientval = "0";
// dsobj = bllobj.BinaryDeSerialize(strDataPath + "\\" + "BinaryClientlocation.dat");
// DataRow[] dr = dsobj.Tables[0].Select("client_location_id in(" + locationid + ")");
// if (dr.Length > 0)
// {
// foreach (DataRow drow in dr)
// {
// retclientval = retclientval + "," + drow["client_id"].ToString();
// }
// }
// return retclientval;
// }
// catch (Exception ex)
// {
// ex.Message.ToString();
// }
//}
}
Csharp:The .dat File using BinaryReader and BinaryWriter Convert to DataTable
最新推荐文章于 2016-12-30 10:34:58 发布