C#在文件读写结构体 Marshal效率低
来源:互联网 发布:数据透视表高级应用 编辑:程序博客网 时间:2024/06/05 17:43
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication2
{
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 class 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;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication2
{
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 class 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;
}
}
}
0 0
- C#在文件读写结构体 Marshal效率低
- C#指针读写结构体,效率高
- C#文件方式读写结构体探析
- C#文件方式读写结构体探析
- 结构体:探析C#文件方式读写结构体
- C#关于使用Marshal.StructureToPtr保存文件
- Marshal C#
- 文件读写(结构体)
- 文件读写(结构体)
- 结构体读写txt文件
- 在C#中如何读写INI文件
- 在C#中读写INI文件
- 在C#中读写XML文件
- 在c#中XML文件的读写
- 如何在C#中读写INI文件
- 呆在宿舍就是效率低
- C# struct class 在Marshal.SizeOf 的区别
- c# Marshal使用
- ajax的坑001
- 官方公开证实(脑控)科技部分佐证资料如下
- 用reflector看到C#Random类的实现
- C#操作指针,用Reflector反编译系统代码看到的
- 换个安逸点儿的英文名,希望以后事业顺遂!
- C#在文件读写结构体 Marshal效率低
- 菜鸟启航:UITextField基本操作(一)
- vs 2010项目转成 2008
- 分享十条nmap实用命令行技巧
- 氢气球制作技术
- 对高管的股权赠予有哪些涉税问题
- 网线总是跳动的问题
- HDU 1043 && POJ 1077 Eight (A*)
- 菜鸟启航:UITextField基本操作(二)