使用c#将结构中的字节写入文件

大家,早安!

我在这里遇到了一个新问题。 我必须写下一个来自我在sistem中声明的结构的数据。

我创建的结构只有两个字段,我用它来稍后将其转换为字节。

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] public struct MyStructData { public short Id; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)] public string Name; } 

我用以下代码转换这个结构的字节:

 private byte[] getBytes(MyStructData aux) { int length = Marshal.SizeOf(aux); IntPtr ptr = Marshal.AllocHGlobal(length); byte[] myBuffer = new byte[length]; Marshal.StructureToPtr(aux, ptr, true); Marshal.Copy(ptr, myBuffer, 0, length); Marshal.FreeHGlobal(ptr); return myBuffer; } 

这个函数用于转换MyStructData类型元素的List结构中的每个元素,其中我有我想要发送到另一台机器的所有寄存器,我使用下面粘贴的代码执行此操作:

 string saveToFilePath = "D:\\" + filename; Stream myStream = myFtpRequest.GetRequestStream(); using (FileStream myFileStream = new FileStream(saveToFilePath, FileMode.Create)) { foreach (MyStructData myData in myStructDataList) { int length = 2048; byte[] newBuffer = new byte[length]; newBuffer = getBytes(myCust); int len = 0; int bytesRead = 0; myFileStream.Write(newBuffer, 0, len); bytesRead += len; } myFileStream.Close(); } 

我的问题是,我看到我的新文件是空的,我不明白为什么它不能获取我的字节信息。 我已经检查了List是否带有数据,我还检查了我的字节转换function是否也能正常工作,但是我无法确定是什么导致我的文件为空。

如果有人知道隧道尽头的灯光,我会非常感谢你的帮助!

编辑现在我有另一种方法将数据写入文件,它运作良好:

 using (Stream stream = new FileStream(saveToFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) { using (BinaryWriter writer = new BinaryWriter(stream, Encoding.Default)) { // get all data foreach (MyStructData myData in myStructDataList) { byte[] newBuffer = getBytes(cd); writer.Write(newBuffer); } } } 

FileStream.Write的第三个参数是要写入的字节数。 它不应该是0。

 string saveToFilePath = "D:\\" + filename; Stream myStream = myFtpRequest.GetRequestStream(); int bytesWritten = 0; using (FileStream myFileStream = new FileStream(saveToFilePath, FileMode.Create)) { foreach (MyStructData myData in myStructDataList) { newBuffer = getBytes(myData); myFileStream.Write(newBuffer, 0, newBuffer.Length); bytesWritten += newBuffer.Length; } } 

我不认为你应该将数据写入这样的文件。 对于像这样的简单结构,最好使用BinaryFormatter

这是一个例子。 首先,您需要通过添加[Serializable]属性来使MyStructData序列[Serializable]

 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] [Serializable] public struct MyStructData { public short Id; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)] public string Name; } 

然后,您可以将这些结构的列表写入文件,如下所示:

 List data = new List(); for (short i = 0; i < 100; ++i) data.Add(new MyStructData{Id = i, Name = i.ToString()}); string filename = "C:\\test\\test.data"; using (var file = File.OpenWrite(filename)) { var writer = new BinaryFormatter(); writer.Serialize(file, data); // Writes the entire list. } 

你可以这样读回来:

 using (var file = File.OpenRead(filename)) { var reader = new BinaryFormatter(); data = (List) reader.Deserialize(file); // Reads the entire list. } foreach (var item in data) Console.WriteLine("Id = {0}, Name = {1}", item.Id, item.Name); 

如果你将编组添加到结构中的唯一原因是你可以在文件中写入和读取它,那么你可以删除它,这样你的结构就像这样:

 [Serializable] public struct MyStructData { public short Id; public string Name; }