我修改了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