日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C#中将结构类型数据存储到二进制文件中方法

發布時間:2023/11/27 生活经验 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#中将结构类型数据存储到二进制文件中方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?????????以往在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);
??????????? }

??????? }
??? }
}



轉載于:https://www.cnblogs.com/kevinGao/p/4188724.html

總結

以上是生活随笔為你收集整理的C#中将结构类型数据存储到二进制文件中方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。