我希望使用 My.Computer.FileSystem.WriteAllBytes 等将由固定长度字符串组成的结构写入文件。
我正在使用带有固定长度字符串的 VB6 项目,我已将其转换为 VB.Net。
Structure Record
<VBFixedString(22),System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,SizeConst:=22)> Public tim() As Char
<VBFixedString(130),System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,SizeConst:=130)> Public des() As Char
<VBFixedString(2),System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,SizeConst:=2)> Public crlf() As Char
End Structure
对 C# 中的编码还是新手,但我如何将此结构获取到字节数组以写入文件。是否有一些编码技巧或者我必须编写自定义方法?
最佳答案
使用.NET框架提供的序列化机制:
Dim formatter As New BinaryFormatter
formatter.Serialize(outputFileStream, objectInstance)
您应该添加<Serializable()>
属性到您的类型。
关于vb.net - 在 .NET 中将结构转换为字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1288966/