简体   繁体   English

如何在.NET和vb6之间读取和编写可互操作的字符串到二进制文件

[英]How to read and write interoperable strings to a binary file between .NET and vb6

I have a new program I am writing in C#, it will need to read and write binary files that are compatible with vb6's binary format. 我有一个用C#编写的新程序,它需要读写与vb6二进制格式兼容的二进制文件。 I can not change the vb6 program but I can make changes to my program. 我无法更改vb6程序,但我可以更改我的程序。

I have everything working except I am a little iffy about strings. 我有一切工作,除了我对字符串有点不确定。

If I write the following in vb6 如果我在vb6中写下以下内容

Private Type PatchRec
   string As String
End Type

Private Sub Command1_Click()
    Dim intFileNum As Integer
    Dim recTest As TestRec
    intFileNum = FreeFile
    Open "E:\testing" & "\" & "testfile" & ".bin" For Binary Access Write As #intFileNum
    recTest.string = "This is a test string"    
    Put #intFileNum, , recPatch
    Close #intFileNum
End Sub

And I write the following in C# 我在C#中写下以下内容

public static void Main(params string[] args)
{
    using(var fs = new FileStream("test.bin", FileMode.Create, FileAccess.ReadWrite))
    using (var bw = new BinaryWriter(fs))
    {
        string str = "This is a test string";
        bw.Write(str);
    }
}

I get these two hex strings 我得到这两个十六进制字符串

vb6 - 15 00 54 68 69 73 20 69 73 20 61 20 74 65 73 74 20 73 74 72 69 6E 67
c# -     15 54 68 69 73 20 69 73 20 61 20 74 65 73 74 20 73 74 72 69 6E 67

It appears BinaryWriter is not the same between the two when writing strings, vb6 uses a two byte string and C# uses a UTF-7 encoded unsigned int. 在编写字符串时,BinaryWriter看起来不一样,vb6使用两个字节的字符串,而C#使用UTF-7编码的unsigned int。 Also I can not find any resource to what encoding VB is using when it writes a file this way. 此外,我无法找到任何资源来编写VB使用这种方式编写文件时使用的编码。

Are there any built in tools to write the same way vb6 does and if not other than turning the length in to a uint16 are there any other gotchas I need to be aware of? 是否有任何内置工具以与vb6相同的方式编写,如果不是将长度转换为uint16,还有其他问题需要注意吗?

It is simply a short that contains the string length, use BinaryWriter(short). 它只是一个包含字符串长度的short,使用BinaryWriter(short)。 Don't use BinaryWriter.Write(string), use BinaryWriter.Write(byte[]) where you got the byte[] from Encoding.Default.GetBytes(). 不要使用BinaryWriter.Write(string),使用BinaryWriter.Write(byte [])从Encoding.Default.GetBytes()获取byte []。

Intentional avoiding the compat functions in the Microsoft.VisualBasic namespace might not the be most productive approach. 故意避免使用Microsoft.VisualBasic命名空间中的compat函数可能不是最有效的方法。 It doesn't byte, the framework doesn't care what language you use. 它不是字节,框架不关心你使用什么语言。 Have a look at Microsoft.VisualBasic.FileSystem.FilePut(). 看看Microsoft.VisualBasic.FileSystem.FilePut()。 Available in any .NET framework version, that's code you won't have to maintain and has been tested by thousands. 可以在任何.NET框架版本中使用,这些代码您不必维护并且已经过数千次测试。 You do have to add an assembly reference to Microsoft.VisualBasic. 您必须向Microsoft.VisualBasic添加程序集引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM