C# 结构体保存自定义文件 实例部分代码
以下四个函数和一个结构体声名;实现操作结构体文件的实例
/// <summary>
/// 导入等值线文件 (主要是读取结构体文件信息)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bt_AsgFileLoadin_Click(object sender, EventArgs e)
{
string file_types = "等值线分级文件(*.els)|*.els;||";
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = file_types;
dlg.Multiselect = false;
if (dlg.ShowDialog() == DialogResult.OK)
{
string strFile = dlg.FileName;
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)];
int iNum = 0;
lv_RangeDisp.Items.Clear();
while (fs.Read(bytData, 0, bytData.Length) > 0)
{
ts = rawDeserialize(bytData);
ListViewItem li = new ListViewItem();
li.UseItemStyleForSubItems = false;
li.SubItems.Clear();
li.SubItems[0].Text = iNum.ToString();
string strValue = ts.dValue.ToString();
li.SubItems.Add(strValue);
li.SubItems.Add("");
int iclr = ts.iClr;
li.SubItems[2].BackColor = System.Drawing.ColorTranslator.FromWin32(iclr);
lv_RangeDisp.Items.Add(li);
}
fs.Close();
}
}
/// <summary>
/// 根据LISTVIEW显示导入为等值线设置文件保存 (主要是定义文件体结构信息)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bt_SaveOther_Click(object sender, EventArgs e)
{
string file_types = "等值线分级文件(*.els)|*.els;||";
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = file_types;
if (dlg.ShowDialog() == DialogResult.OK)
{
string strFile = dlg.FileName;
FileStream fs = new FileStream(strFile, FileMode.Create, FileAccess.Write);
string strColor = "";
string strValue = "";
string strType = cb_Type.Text.ToString();
for (int ig = 0; ig < lv_RangeDisp.Items.Count; ig++)
{
try
{
TestStruct ts = new TestStruct();
strValue = this.lv_RangeDisp.Items[ig].SubItems[1].Text.ToString();
strColor = System.Drawing.ColorTranslator.ToWin32(this.lv_RangeDisp.Items[ig].SubItems[2].BackColor).ToString();
ts.strName = strType;
ts.dValue = Convert.ToDouble(strValue);
ts.iClr = Convert.ToInt32(strColor);
byte[] bytData = rawSerialize(ts);
fs.Write(bytData, 0, bytData.Length);
}
catch
{
}
}
fs.Close();
MessageBox.Show("等值线分级配置文件导出成功");
}
}
/// <summary>
/// 等值线结构体 (声名结构体)
/// </summary>
[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 double dValue;
public int iClr;
public string strName;
//[MarshalAs(UnmanagedType.R8)] //,FieldOffset(0)]
//public double dValue;
//[MarshalAs(UnmanagedType.R8)] //, FieldOffset(8)]
//public double dClr;
//[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] //, FieldOffset(10)]
//public byte[] strName;
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="rawdatas"></param>
/// <returns></returns>
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;
}