using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
namespace RWFile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//从文件中读结构体
private void button1_Click(object sender, EventArgs e)
{
string strFile = Application.StartupPath + "\\test.dat";
if (!File.Exists(strFile))
{
MessageBox.Show("文件不存在");
return;
}
FileStream fs = new FileStream(strFile, FileMode.Open,
FileAccess.ReadWrite);
TestStruct ts = new TestStruct();
byte[] bytData = new byte[Marshal.SizeOf(ts)];
fs.Read(bytData, 0, bytData.Length);
fs.Close();
ts = rawDeserialize(bytData);
textBox1.Text = ts.dTest.ToString();
textBox2.Text = ts.uTest.ToString();
textBox3.Text = Encoding.Default.GetString(ts.bTest);
}
//向文件中写结构体
private void button2_Click(object sender, EventArgs e)
{
string strFile = Application.StartupPath + "\\test.dat";
FileStream fs = new FileStream(strFile, FileMode.Create , FileAccess.Write);
TestStruct ts = new TestStruct();
ts.dTest = double.Parse(textBox1.Text);
ts.uTest = UInt16.Parse(textBox2.Text);
ts.bTest = Encoding.Default.GetBytes(textBox3.Text);
byte[] bytData = rawSerialize(ts);
fs.Write(bytData, 0, bytData.Length);
fs.Close();
}
[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Ansi)] //,Size=16
public struct TestStruct
{
[MarshalAs(UnmanagedType.R8)] //,FieldOffset(0)]
public double dTest;
[MarshalAs(UnmanagedType.U2)] //, FieldOffset(8)]
public UInt16 uTest;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] //, FieldOffset(10)]
public byte[] bTest;
}
//序列化
public static byte[] rawSerialize(object obj)
{
int rawsize = Marshal.SizeOf(obj);
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Marshal.StructureToPtr(obj, buffer, false);
byte[] rawdatas = new byte[rawsize];
Marshal.Copy(buffer, rawdatas, 0, rawsize);
Marshal.FreeHGlobal(buffer);
return rawdatas;
}
//反序列化
public static TestStruct rawDeserialize(byte[] rawdatas)
{
Type anytype = typeof(TestStruct);
int rawsize = Marshal.SizeOf(anytype);
if (rawsize > rawdatas.Length) return new TestStruct();
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Marshal.Copy(rawdatas, 0, buffer, rawsize);
object retobj = Marshal.PtrToStructure(buffer, anytype);
Marshal.FreeHGlobal(buffer);
return (TestStruct)retobj;
}
}
}