-1

我正在尝试读取二进制文件,将其转换为字符串,然后将其写入磁盘。

基本上这是我的代码

Dim bytes As Byte() = System.IO.File.ReadAllBytes("C:\1.exe")
Clipboard.SetText(bytes.ToString)
System.IO.File.WriteAllBytes("C:\2.exe", bytes)

它适用于读/写文件,但无法将字节复制到剪贴板,我得到的是System.Byte[]剪贴板上的“”..那么我怎样才能读取一些二进制文件字节并将它们存储/复制到剪贴板?

4

3 回答 3

2

当您使用 Clipboard.SetText() 时,您应该将文本放在剪贴板上。EXE 文件不包含文本,它包含一个程序。尝试用记事本打开你的 1.exe 文件,看看它是什么样子的。不是文字。

可以将二进制文件放在剪贴板上。这可能看起来像这样:

    Dim files = New String() {"c:\windows\notepad.exe"}
    Clipboard.SetData(DataFormats.FileDrop, files)

现在切换到 Windows 资源管理器并按 Ctrl+V。你有一个 Notepad.exe 的副本

如果您只想要文件的副本,请使用 File.Copy():

    System.IO.File.Copy("c:\1.exe", "c:\2.exe")

它不仅复制文件中的字节,还确保文件的属性设置正确。

于 2013-04-19T16:23:53.223 回答
1
bytes.ToString

takes the string representation of the array, rather than its contents. Reference types return their typename from ToString(), unless they override it to do something different.

What you're really looking to do is convert the byte array to a string. For this, use Encoding.GetString(bytes) - where you'll need to use a static member of Encoding like Encoding.UTF8 as the appropriate instance - depending on what representation of a string your bytes encode.

edit #1: I didn't actually notice what you were reading. What do you expect the text representation of an executable file to be? What meaning is there to placing it on the clipboard?

于 2013-04-19T16:16:43.217 回答
1

您需要将字节转换为字符串:

Dim text as String = System.Text.Encoding.UTF8.GetString(bytes)
Clipboard.SetText(bytes.ToString)

但是,正如 Neolisk 所提到的,这并没有真正的帮助,因为您实际上是在阅读二进制文件,而不是文本文件

于 2013-04-19T16:15:12.223 回答