[英]C# equivalent to VB6's 'Open' & 'Put' Functions
I will try to make this as straight forward as possible. 我将尝试使这一过程尽可能直接。 This question does not simply involve reading and writing bytes.
这个问题不仅仅涉及读写字节。 I am looking for an exact translation between this VB6 code and C# code.
我正在寻找此VB6代码和C#代码之间的确切转换。 I know this is not always a posibility but I'm sure someone out there has some ideas!
我知道这并不总是可行的,但我敢肯定有人在那里有一些想法!
VB6 Code & explanation: VB6代码和说明:
The below code writes data into a specific part of the file. 下面的代码将数据写入文件的特定部分。
[ Put [#]filenumber, [byte position], varname ].
It is the *byte position * that I am having trouble figuring out - and help with this would be very much appreciated! 这是我无法确定的* byte位置*-非常感谢您的帮助!
Dim file, stringA as string
Open file for Binary As #1
lPos = 10,000
stringA = "ThisIsMyData"
Put #1, lPos, stringA
Close #1
So, I am looking for some help with the byte position, once again. 因此,我再次寻求有关字节位置的帮助。 In this example the byte position was represented by lPos.
在此示例中,字节位置由lPos表示。
EDIT FOR HENK - 编辑为汉克-
I will be reading binary data. 我将读取二进制数据。 There are some characters in this binary data that I will need to replace .
我需要替换此二进制数据中的某些字符。 For this reason, I will be using VB6's instr function to get the poisition of this data (there lengths are previously known).
因此,我将使用VB6的instr函数来获取此数据的位置(以前知道长度)。 I will then use Vb6's Put function to write this data at the newfound position.
然后,我将使用Vb6的Put函数在新找到的位置写入此数据。 This will overwrite the old data with the new data.
这将用新数据覆盖旧数据。 I hope this helped!
希望对您有所帮助!
If it helps anyone, here is some further information regarding the Put
function. 如果对任何人有帮助, 这里是有关
Put
函数的更多信息。
Thanks so much, Evan 非常感谢,埃文
Can you not use a BinaryWriter ? 您不能使用BinaryWriter吗?
For example: 例如:
FileStream fs = new FileStream(file, FileMode.Open);
BinaryWriter w = new BinaryWriter(fs);
w.Seek(10000, SeekOrigin.Origin);
w.Write(encoding.GetBytes("ThisIsMyData"));
w.Flush();
w.Close();
fs.Close();
You can do this using StreamReader and StreamWriter. 您可以使用StreamReader和StreamWriter进行此操作。
I would try something like this: 我会尝试这样的事情:
This question is not a perfect fit, however it shows a similiar technique using text (not binary data): Insert data into text file 这个问题不是一个很好的选择,但是它显示了使用文本(而非二进制数据)的类似技术:将数据插入文本文件
看看在StreamWriter类专门在这个过载的Write方法 ,它允许你开始写作到流中的特定位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.