我的目標是將 txt 檔案存盤在 VB.Net 應用程式的 Resources 檔案夾中,
我使用 Project > Project Properties > Resources > Add Resources > Add Existing File 添加了 Dir.txt 檔案。
我可以看到該檔案,并且可以使用非常奇怪的幾行代碼讀取它,這些代碼涉及使用硬編碼路徑。不理想。
我已經包含了一些我已經測驗過沒有結果的代碼
我可以使用 OpenFileDialog 但我不想去釣魚檔案!
只需單擊一個按鈕并在 RichTextBox 中顯示檔案
因為專案有一個 SQLite 我考慮在資料庫創建功能期間將檔案添加到它自己的表中
我的兩個問題是
如果當我使用 Inno Setup 打包檔案時檔案在資源檔案夾中,該檔案是否會在 exe 檔案中?
我們能否解釋一種更好的方法來完成單擊按鈕并將檔案添加到 RichTextBox 中?
是的,這些是進口的
Imports System.IO
Imports System.Reflection
Private Sub btnGetCode_Click(sender As Object, e As EventArgs) Handles btnGetCode.Click
'Dim openFileDialog As OpenFileDialog = New OpenFileDialog()
'Dim dr As DialogResult = openFileDialog.ShowDialog()
'If dr = System.Windows.Forms.DialogResult.OK Then
' Dim openPath As String = OpenFileDialog.FileName
' rtbViewCode.Text = File.ReadAllText(openPath)
'End If '' This works but I do not want to go Fishing for the File
'Why Does this code BELOW FAIL
'Dim openPath = Path.Combine(Application.StartupPath, "Resource.Dir.txt")
'rtbViewCode.Text = File.ReadAllText(openPath)
'Why Does this code BELOW FAIL
'Dim assmbly As Assembly = Assembly.GetExecutingAssembly()
'Dim reader As New StreamReader(assmbly.GetManifestResourceStream("frmViewFile.vb.Dir.txt"))
'rtbViewCode.Text = reader.ReadToEnd
'While this Code WORKS
Dim openP As String = "C:\Users\Dwight\source\repos\TestTwoGrids\TestTwoGrids\Resources\Dir.txt"
rtbViewCode.Text = File.ReadAllText(openP)
'End If
End Sub
uj5u.com熱心網友回復:
如果您已經添加了Dir.txt資源檔案 ( Project > Project Properties > Resources > Add Resources > Add Existing File) 并分配了別名,假設DirTxt

剩下要做的就是將其內容插入到 RichBox 中,如下所示:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RichTextBox1.Text = My.Resources.DirTxt
End Sub
是的,一旦你編譯它,資源就會包含在可執行檔案中。
如果這對您有幫助,請將其標記為答案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/426206.html
