FilePut does not add two bytes of length when writing a string to a binary file from C #. FilePutObject throws exceptions from classes / structs

I have an application that needs to be both read and write compatible with the VB6 binary. I found out about the Microsoft.VisualBasic.FileSystem class, and I'm trying to write a structure to a file.

 FileSystem.FileOpen(file, "test.bin", Microsoft.VisualBasic.OpenMode.Binary); FileSystem.FilePut(file, patch.intTest); FileSystem.FilePut(file, patch.dateTest); FileSystem.FilePut(file, patch.stringTest, StringIsFixedLength: false); FileSystem.FilePut(file, patch.boolTest); 

Everything is written correctly if I put each element separately, except for a line. When a VB6 application writes a string, it adds two bytes of length, my code does not.

At MSDN, he says binary mode removes length bytes if it is not in the structure. Setting StringIsFixedLength to true should do this, but it seems that binary mode exceeds this parameter.

I am trying to use FilePutObject and trying to pass a structure or class (this is what msdn says you need to do to display bytes), it throws an exception

 System.ArgumentException occurred Message=File I/O with type 'PatchFileStructure' is not valid. Source=Microsoft.VisualBasic StackTrace: at Microsoft.VisualBasic.FileSystem.FilePutObject(Int32 FileNumber, Object Value, Int64 RecordNumber) at SandboxConsole.Sandbox.Main(String[] args) in E:\Code\Sandbox Console\SandboxConsole\Program.cs:line 41 InnerException: 

Full code

 using System; using Microsoft.VisualBasic; namespace SandboxConsole { static class Sandbox { public struct PatchFileStructure { public Int32 intTest { get; set; } public DateTime dateTest { get; set; } public string stringTest { get; set; } public bool boolTest { get; set; } } public static void Main(params string[] args) { var patch = new PatchFileStructure() { intTest = 5, dateTest = new DateTime(1999, 1, 1, 1, 1, 1), stringTest = "Test Name", boolTest = true, }; int file = FileSystem.FreeFile(); FileSystem.Kill("test.bin"); FileSystem.FileOpen(file, "test.bin", Microsoft.VisualBasic.OpenMode.Binary); FileSystem.FilePutObject(file, patch); //FileSystem.FilePut(file, patch.intTest); //FileSystem.FilePut(file, patch.dateTest); //FileSystem.FilePut(file, patch.stringTest, StringIsFixedLength: false); //FileSystem.FilePut(file, patch.boolTest); FileSystem.FileClose(file); } } } 

Here are my two files that I am comparing. Spaces are added to the C # version to illustrate the problem.

 vb6 - 05 00 00 00 24 F6 1D 5B 21 A8 E1 40 09 00 54 65 73 74 20 4E 61 6D 65 FF FF C# - 05 00 00 00 24 F6 1D 5B 21 A8 E1 40 54 65 73 74 20 4E 61 6D 65 FF FF 

If I try to read a vb6 file with C # everything works fine except for a line that returns null.

+2
source share
3 answers

FilePutObject seems a lot more verbose and writes out more metadata than is needed, primarily for options.
Have you tried simply:

 FileSystem.FilePut(file, patch); 

In this case, the "ValueType" overload is used.

+3
source

You can write the length explicitly before the line:

 FileSystem.FilePut(file, patch.stringTest.Lenght.ToInt16); FileSystem.FilePut(file, patch.stringTest, StringIsFixedLength: false); 
+2
source

Change to:

 FileSystem.FilePut(file, patch.stringTest, StringIsFixedLength: false); 

The system does not record the length prefix because you say that the string has a fixed length.

+1
source

All Articles