如何以二进制模式打开文件并替换十六进制字符串?
我需要以二进制模式打开并编辑可执行文件,以将十六进制值替换为字符串。
在 PHP 中,看起来像这样:
<?php
$fp = fopen('file.exe', 'r+');
$content = fread($fp, filesize('file.exe'));
fclose($fp);
print $content;
/* [...] This program cannot be run in DOS mode.[...] */
?>
How I get it in C#?
I need open and edit a executable file in binary mode, to replace hex value as string.
In PHP, looks like this:
<?php
$fp = fopen('file.exe', 'r+');
$content = fread($fp, filesize('file.exe'));
fclose($fp);
print $content;
/* [...] This program cannot be run in DOS mode.[...] */
?>
How I get it in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
File.ReadAllBytes
将文件的字节读取为字节数组。如果你想将其转换为十六进制字符串(我一般建议不要这样做 - 字符串在 C# 中是不可变的,因此即使修改单个字节也需要复制字符串的其余部分),你可以使用:
Use
File.ReadAllBytes
to read the bytes of a file as a byte array.If you want to convert this to a hex string (and I'd in general I'd advise against doing so - strings are immutable in C# so modifying even a single byte will require copying the rest of the string) you can for example use:
您询问有关写入文件的问题,但您的 PHP 代码是用于读取的。要处理文件,您可以使用 FileStream 类
You ask about writting to file, but you PHP code is for reading. For working with files you can use FileStream class