我在我的 VB.NET 2019 專案中嵌入了一個資源檔案,我想通過單擊按鈕將其復制到桌面。我試圖用一個私人子做這個,所以我可以重用這個功能,但我不能讓它作業。這些檔案是二進制檔案。
Private Sub CreateBinary(ByVal objResource As Object, ByVal FullPath As String)
Dim WriteBinary As BinaryWriter
Try
WriteBinary = New BinaryWriter(FullPath)
WriteBinary.Write(objResource)
WriteBinary.Close()
Catch
End Try
End Sub
然后我想從以下呼叫:
CreateBinary(My.Resources.user32, My.Computer.FileSystem.SpecialDirectories.Desktop)
我似乎找不到任何有用的東西。我與其他資源檔案有類似的功能,這些資源檔案是使用StreamWriter. 所有這一切確實證實了我的二進制檔案作為資源檔案正確嵌入,因為它們都在我的專案中的同一個位置。
uj5u.com熱心網友回復:
首先要檢查的是如何在適當的 resx 檔案中定義資源(對于 VB,我認為它將在“我的專案”中)。它應該看起來像這樣:
<value>[path to source file];System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
如果是這樣,那么設計器提供的資源類會給你一個位元組陣列,這對于寫入檔案來說是微不足道的:
System.IO.File.WriteAllBytes(filename, My.Resources.FileResource)
(其中“FileResource”是資源的名稱)
uj5u.com熱心網友回復:
嘗試以下
Imports System.IO
Module ResourceExtensions
<Runtime.CompilerServices.Extension()>
Public Sub FileSave(BytesToWrite() As Byte, FileName As String)
If File.Exists(FileName) Then
File.Delete(FileName)
End If
Dim FileStream As New FileStream(FileName, FileMode.OpenOrCreate)
Dim BinaryWriter As New BinaryWriter(FileStream)
BinaryWriter.Write(BytesToWrite)
BinaryWriter.Close()
FileStream.Close()
End Sub
End Module
假設資源中的檔案是Customers.xlsx,這里我解壓到C:\ExcelFiles。路徑必須存在。
Public Class Form1
Private Sub ExtractButton_Click(sender As Object, e As EventArgs) _
Handles ExtractButton.Click
Dim exportFileName = "C:\ExcelFiles\Customers.xlsx"
My.Resources.Customers.FileSave(exportFileName)
End Sub
End Class
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/490860.html
上一篇:我如何讓這個文本框實時更新
下一篇:可變寬度StackView?
