c# 存取结构体 二进制文件
2018-03-21 10:10
483 查看
转载连接: http://www.cnblogs.com/baishahe/archive/2010/03/18/1688995.html
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
namespace StructTest
{
public partial class Form1 : Form
{
string filename = @"d:\poi.st";
#region 结构体
//90
[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 Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MY_STRUCT[] arr = new MY_STRUCT[2];
MY_STRUCT np = new MY_STRUCT();
np.x = 114.123456;
np.y = 23.56789;
np.Name = "珠海市政府";
np.PointID = Convert.ToInt64(1234);
np.TypeCode = Convert.ToInt64(65);
arr[0] = np;
np = new MY_STRUCT();
np.x = 115.123456;
np.y = 24.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);
}
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 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);
}
}
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;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
namespace StructTest
{
public partial class Form1 : Form
{
string filename = @"d:\poi.st";
#region 结构体
//90
[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 Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MY_STRUCT[] arr = new MY_STRUCT[2];
MY_STRUCT np = new MY_STRUCT();
np.x = 114.123456;
np.y = 23.56789;
np.Name = "珠海市政府";
np.PointID = Convert.ToInt64(1234);
np.TypeCode = Convert.ToInt64(65);
arr[0] = np;
np = new MY_STRUCT();
np.x = 115.123456;
np.y = 24.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);
}
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 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);
}
}
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;
}
}
}
相关文章推荐
- c# 存取结构体二进制文件
- C#存取SQL Server数据库之一:二进制存取图片文件
- C#以二进制方式存取文件
- (转帖)C#存取SQL Server数据库之一:二进制存取图片文件
- C#二进制存取图片文件
- C# 向数据中存储和读取二进制文件!!
- c#将文件以二进制形式存入数据库
- C#中oracle二进制文件操作(新增、修改)
- C# 文件与二进制互转数据库写入读出
- 利用DataSet存取SQL Server中的二进制文件
- c# 存取结构体 二进制文件
- C#实现文件二进制存储
- C# 通过二进制,将多个文件合并为一个。
- .NET下对二进制文件进行加密解密(C#)
- 二进制文件存取示例(T-SQL)
- C#按二进制模式读写文件
- C#读取二进制文件浅析
- C#读取二进制文件浅析
- C# 向数据中存储和读取二进制文件!!
- java存取二进制数据到xml文件