C#中将结构类型数据存储到二进制文件中方法
来源:互联网 发布:sql insert case when 编辑:程序博客网 时间:2024/04/30 09:29
以往在vb6,vc6中都有现成的方法将结构类型数据写入和读取到二进制文件中,但是在c#中却没有现成的方法来实现,因此我查阅了一些资料,借鉴了网上一些同学的做法,自己写了个类似的例子来读写结构类型数据到二进制文件中,废话不多说了,先上代码:
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string filename = @"d:\testbinary.st";
#region 结构体
[StructLayout(LayoutKind.Sequential), Serializable]
public struct MY_STRUCT
{
public double x; //点的经度坐标
public double y; //点的纬度坐标
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 40)]
public string Name; //Name[40]; //名称
public long PointID; //点的ID号
public long TypeCode; //客户不使用该字段
}
#endregion
public void WriteInfo(byte[] bt)
{
if (File.Exists(filename))
{
File.Delete(filename);
return;
}
FileStream fs = new FileStream(filename, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bt);
bw.Flush();
bw.Close();
fs.Close();
MessageBox.Show("保存成功!");
}
public byte[] ReadInfo(string file)
{
FileStream fs = new FileStream(file, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bt = br.ReadBytes(144);
br.Close();
fs.Close();
return bt;
}
private MY_STRUCT Byte2Struct(byte[] arr)
{
int structSize = Marshal.SizeOf(typeof(MY_STRUCT));
IntPtr ptemp = Marshal.AllocHGlobal(structSize);
Marshal.Copy(arr, 0, ptemp, structSize);
MY_STRUCT rs = (MY_STRUCT)Marshal.PtrToStructure(ptemp, typeof(MY_STRUCT));
Marshal.FreeHGlobal(ptemp);
return rs;
}
private byte[] Struct2Byte(MY_STRUCT s)
{
int structSize = Marshal.SizeOf(typeof(MY_STRUCT));
byte[] buffer = new byte[structSize];
//分配结构体大小的内存空间
IntPtr structPtr = Marshal.AllocHGlobal(structSize);
//将结构体拷到分配好的内存空间
Marshal.StructureToPtr(s, structPtr, false);
//从内存空间拷到byte数组
Marshal.Copy(structPtr, buffer, 0, structSize);
//释放内存空间
Marshal.FreeHGlobal(structPtr);
return buffer;
}
private void button1_Click(object sender, EventArgs e)
{
MY_STRUCT[] arr = new MY_STRUCT[2];
MY_STRUCT np = new MY_STRUCT();
np.x = 112.123456;
np.y = 21.56789;
np.Name = "深圳市政府1";
np.PointID = Convert.ToInt64(1234);
np.TypeCode = Convert.ToInt64(65);
arr[0] = np;
np = new MY_STRUCT();
np.x = 113.123456;
np.y = 22.56789;
np.Name = "深圳市政府2";
np.PointID = Convert.ToInt64(1235);
np.TypeCode = Convert.ToInt64(66);
arr[1] = np;
int structSize = Marshal.SizeOf(typeof(MY_STRUCT));
byte[] temp = new byte[structSize * arr.Length];
byte[] temp1 = Struct2Byte(arr[0]);
byte[] temp2 = Struct2Byte(arr[1]);
Array.Copy(temp1, 0, temp, 0, temp1.Length);
Array.Copy(temp2, 0, temp, structSize, temp2.Length);
WriteInfo(temp);
}
private void button2_Click(object sender, EventArgs e)
{
byte[] bt = ReadInfo(filename);
int structSize = Marshal.SizeOf(typeof(MY_STRUCT));
int num = bt.Length / structSize;
for (int i = 0; i < num; i++)
{
byte[] temp = new byte[structSize];
Array.Copy(bt, i * structSize, temp, 0, structSize);
MY_STRUCT np = new MY_STRUCT();
np = Byte2Struct(temp);
}
}
}
}
- C#中将结构类型数据存储到二进制文件中方法
- C#中将数据导到excel中
- 关于J2ME中将图片数据存储到本地的方法
- C# 向数据中存储和读取二进制文件!!
- C++中将csv文件中的数据存储到数组中
- C#中将DataTable中数据导出到csv文件中
- C#中将DataTable中数据导出到csv文件中
- C#中将DataTable中数据导出到csv文件中
- C#中将DataTable中数据导出到csv文件中
- C#中将DataTable中数据导出到csv文件中
- c#中将数据库数据导出到EXCEL中
- C#中将ListView中数据导出到Excel代码
- 在C#中将任意数值类型数据与字节数组相互转换的一个方法
- c++中将数据存储到.csv文件中(类似于xsl表格)
- C++中将vector<float>的数据进行base64加密存储到数据库中(内含base64源码)
- php中将数据加入到文件中
- 将double类型的数据写入二进制文件,从二进制文件中读取double类型数据
- 在C#中存储Blob类型的数据,
- 画了个Android——Canvas类的使用
- 淘宝商城model部分
- bbed初体验
- android 水平button等宽
- git 的本地基本操作
- C#中将结构类型数据存储到二进制文件中方法
- Java常用工具类三 -- 正则表达式
- alsa 音频库的移植
- 查了查jtag和串口的区别
- android开发TextView中一些属性的设置
- Binary Tree Postorder Traversal
- linux里的命令
- Clone Graph
- Java异常机制,你知道吗?