Error copying from resources

时间:2015-10-30 22:55:53

标签: vb.net resources copy

While copying from resources to a folder under appdata folder: i get an error, but I'm not finding any mistake in code..

Private Sub Help_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    File.WriteAllBytes(MainPath & "\Help.rtf", My.Resources.HelpRTF)

    Dim HelpRTF = (MainPath & "\Help.rtf")
    Helpbox.LoadFile(HelpRTF)
End Sub

HelpRTF is a .rtf file, MainPath is a directory under %appdata% folder

Error: Value of type 'String' cannot be converted to 'Byte()'.

Error at: My.Resources.HelpRTF

1 个答案:

答案 0 :(得分:0)

您收到该错误的原因是File.WriteAllBytes()方法的第二个参数需要Byte(),而不是String。如果要将文本(String)写入文件,可以使用File.WriteAllText()方法。

由于RTF可以包含图像,文本等将其视为文本可能会破坏它并且不用说,可能会出现编码问题。因此,不要使用File.WriteAllText()方法,而是将FileType资源的HelpRTF更改为Binary而不是Text,如下所示:

Screenshot

之后,您可以按原样使用您的代码:

Private Sub Help_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    File.WriteAllBytes(MainPath & "\Help.rtf", My.Resources.HelpRTF)
    Dim HelpRTF = (SWinPath & "\Help.rtf")
    Helpbox.LoadFile(HelpRTF)
End Sub

参考文献: