I'm done searching any topic on stackoverflow to solving my problem, but i still can't solving my problem with my code I want to reading binary file using c# , and make a struct for it
here is the code
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Auto)]
public struct YourStruct
{
[FieldOffset(44)]
public int none; // 4 bytes
[FieldOffset(48)]
//[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 156)]
[MarshalAs(UnmanagedType.LPTStr, SizeConst = 156)]
public String alldata;
}
private void button1_Click(object sender, EventArgs e)
{
string strFilePath = @"F:\dat\bintest.bin";
FileStream stream = File.OpenRead(strFilePath);
YourStruct aStruct;
//int count = Marshal.SizeOf(typeof(YourStruct));
int count = 791616;
byte[] readBuffer = new byte[count];
BinaryReader reader = new BinaryReader(stream);
readBuffer = reader.ReadBytes(count);
GCHandle handle = GCHandle.Alloc(readBuffer, GCHandleType.Pinned);
aStruct = (YourStruct)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(YourStruct));
handle.Free();
Console.WriteLine("None = {0}", aStruct.none);
Console.WriteLine("Data : {0}", aStruct.alldata);
}
The problem is , when i run this project, aStruct.alldata show nothing , like didn't read the file
Any solution for this ? Thanks!
答案 0 :(得分:0)