C# 相当于 VB6 的“Open” & “放”功能

发布于 2024-11-16 01:41:18 字数 874 浏览 4 评论 0原文

我会尽力让这件事尽可能简单。这个问题并不简单地涉及读取和写入字节。我正在寻找此 VB6 代码和 C# 代码之间的精确翻译。我知道这并不总是可行,但我确信有人有一些想法!

VB6代码&解释:

以下代码将数据写入文件的特定部分。

[ Put [#]filenumber, [byte position], varname ].

这是我无法弄清楚的*字节位置* - 非常感谢您提供帮助!

Dim file, stringA as string

Open file for Binary As #1
    lPos = 10,000
    stringA = "ThisIsMyData"
Put #1, lPos, stringA

Close #1

因此,我再次寻求有关字节位置的帮助。在此示例中,字节位置由 lPos 表示。

编辑 HENK -

我将读取二进制数据。我需要替换此二进制数据中的一些字符。因此,我将使用 VB6 的 instr 函数来获取该数据的位置(长度是先前已知的)。然后,我将使用 Vb6 的 Put 函数将此数据写入新找到的位置。这将用新数据覆盖旧数据。我希望这有帮助!

如果它对任何人有帮助,这里是一些进一步的有关 Put 函数的信息。

非常感谢, 埃文

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. I know this is not always a posibility but I'm sure someone out there has some ideas!

VB6 Code & explanation:

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!

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.

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). I will then use Vb6's Put function to write this data at the newfound position. 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.

Thanks so much,
Evan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染柒℉ 2024-11-23 01:41:18

您不能使用 BinaryWriter 吗?

例如:

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();

Can you not use a 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();
昇り龍 2024-11-23 01:41:18

您可以使用 StreamReader 和 StreamWriter 来完成此操作。

我会尝试这样的事情:

  • 读取前 n 位并使用 StreamWriter 将它们写入新的流中。
  • 使用相同的 StreamWriter,写入要插入的新位。
  • 最后,写入 StreamReader 中的其余位。

这个问题并不完美,但它显示了使用文本(不是二进制数据)的类似技术: 将数据插入文本文件

You can do this using StreamReader and StreamWriter.

I would try something like this:

  • Read the first n bits and write them into a new stream using StreamWriter.
  • Using the same StreamWriter, write the new bits that you want to insert.
  • Finally, write the rest of the bits from your StreamReader.

This question is not a perfect fit, however it shows a similiar technique using text (not binary data): Insert data into text file

永不分离 2024-11-23 01:41:18

专门针对此重载查看 StreamWriter 类 Write 方法,它允许您开始写入特定位置内 溪流。

Take a look at the StreamWriter Class specially at this overload of the Write method, which allows you to start writing to a specific place within the stream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文