我修改了Greg Bogumil从上面的答案来创建扩展方法。 现在任何二进制类型的资源文件,我可以简单的做这样的事情......
My.Resources.some_resource_file.ExtractResourceToDisk(newpath)
注意,您必须将以下函数添加到模块,您不能创建一个类内部的扩展方法。
''' <summary>
''' Extracts the binary resource file and saves it to the specified location.
''' </summary>
''' <param name="Resource">[Byte()] The binary resource file byte array.</param>
''' <param name="FileToExtractTo">[String] The full file path of the new file to be saved.</param>
''' <returns>[Boolean] Returns True on success.</returns>
<Extension>
Public Function ExtractResourceToDisk(ByVal Resource As Byte(), ByVal FileToExtractTo As String) As Boolean
Try
Using ms As New MemoryStream(Resource)
Using ResourceFile As New FileStream(FileToExtractTo, FileMode.Create)
Dim b(ms.Length) As Byte
ms.Read(b, 0, ms.Length)
ResourceFile.Write(b, 0, b.Length - 1)
ResourceFile.Flush()
ResourceFile.Close()
End Using
End Using
Return True
Catch ex As Exception
Return False
End Try
End Function
美丽的解决方案!它在第一次尝试中工作。你们是如此聪明:p – 2010-04-08 23:58:09