使用 IronBarcode 建立 C# MSI 安裝程式
若要使用 IronBarcode 建立 MSI 安裝程式,請在您的解決方案中新增一個"Setup Project",加入所需的 DLL 檔案(IronBarcodeInterop.dll 及 ReaderInterop.dll),並編譯專案以產生可分發的 MSI 套件。
MSI(Microsoft Installer)是一種 Windows 安裝套件,用於協助軟體的安裝、更新及移除。 使用 MSI 提供了一種標準化的應用程式安裝方法,這對於 Enterprise 部署尤其有益。 MSI 格式支援諸如回滾功能、管理安裝點以及 Windows Installer 服務整合等進階功能。
IronBarcode 提供工具,可與您的現有應用程式無縫整合,並將其轉換為 MSI 檔以便輕鬆分發。 它確保能在各種環境中可靠地安裝,並允許開發人員選擇要包含或排除哪些元件。 此函式庫支援多種BarCode格式,使其能廣泛應用於各類商業場景。
本教學將示範如何透過一個利用 IronBarcode 強大掃描功能的 BarCode 應用程式範例,來建立 MSI 檔案。
快速入門:一鍵生成並讀取 MSI BarCode
使用 IronBarcode 的簡易 API,只需最少的設定即可建立和讀取 MSI 條碼。以下程式碼片段展示了寫入 MSI 條碼圖像並讀取回來的過程是多麼簡單——僅需幾行程式碼即可完成。
-
using NuGet 套件管理員安裝 https://www.nuget.org/packages/BarCode
PM > Install-Package BarCode -
請複製並執行此程式碼片段。
var msiImg = IronBarCode.BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.MSI).SaveAsImage("msi.png"); var results = IronBarCode.BarcodeReader.Read("msi.png", new BarcodeReaderOptions { ExpectBarcodeTypes = BarcodeEncoding.MSI }); -
部署至您的生產環境進行測試
立即透過免費試用,在您的專案中開始使用 IronBarcode
開始之前需要哪些先決條件?
在開始專案之前,請先下載 Microsoft Visual Studio Installer Projects 擴充套件 才能使 MSI 建置正常運作。 此外,請確保您已透過 NuGet 在專案中安裝 IronBarcode。
為何需要 Visual Studio Installer Projects 擴充套件?
此擴充功能提供了在 Visual Studio 2022 中建立 MSI 安裝程式所需的"Setup Project"範本。此擴充功能新增了自 Visual Studio 2010 版本後遭移除的部署專案範本,讓開發人員能夠建立傳統的 Windows Installer 套件。
我應該針對哪個版本的 .NET Framework 進行翻譯?
請使用 Windows Forms App (.NET Framework) 以確保與 MSI 部署情境具備最佳相容性。 雖然 IronBarcode 支援多種 .NET 平台,但 .NET Framework 版本能確保與 Windows 系統擁有最廣泛的相容性,而 MSI 安裝程式通常部署於此類系統中。
如何建立初始的 MSI 安裝程式專案?
在此範例中,請使用 Windows Forms App(.NET Framework)專案來展示其功能。 此方法為桌面BarCode掃描應用程式提供了熟悉的使用者介面範式。
哪種專案類型最適合 MSI 安裝程式?
Windows Forms 應用程式是建立透過 MSI 部署之 BarCode 掃描應用程式的最直接途徑。 這些工具提供原生 Windows 整合功能,且除了 .NET Framework 之外,無需額外的執行階段依賴項。
示範應用程式的關鍵組件有哪些?
此應用程式包含一個表單,表單上的按鈕會開啟檔案對話方塊,以便從圖片中掃描BarCode。 此簡易介面在將部署複雜度降至最低的同時,亦能展示核心功能。 若需處理更進階的應用情境,建議探索從 PDF 讀取 BARCODE,或實作非同步 BARCODE 讀取功能。
如何在 Windows 表單中新增按鈕?
- 導航至
ToolBox - 搜尋
Button - 透過拖放操作將按鈕新增至 Windows 表單

哪裡可以找到按鈕控制項?
Button 控制項位於 Visual Studio 工具箱的"常用控制項"區段中。 若工具箱未顯示,請從 View > Toolbox 開啟,或按下 Ctrl+Alt+X。
我該如何在表單上定位這個按鈕?
請將按鈕置於表單中央,或將其放置在使用者自然預期會找到主要操作的位置。 請參考 Windows UI 設計準則,以確保跨應用程式的使用者體驗一致性。
如何編輯按鈕程式碼以處理BarCode掃描?
雙擊按鈕元件即可存取表單的 C# 程式碼。 以下是表單元件的邏輯——它接收BARCODE並嘗試進行掃描。 此程式碼僅能掃描圖片,不適用於 PDF 檔案。 針對 PDF 文件,請使用 ReadPdf 方法。 如需完整的BarCode讀取選項,請參閱BarCode讀取器設定文件。
using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MsiInstallerSample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IronSoftware.Logger.LoggingMode = IronSoftware.Logger.LoggingModes.All;
IronSoftware.Logger.LogFilePath = "Default.log";
IronBarCode.License.LicenseKey = "IRONBARCODE-MYLICENSE-KEY-1EF01";
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "Image files (All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
// Load the selected image
using (Bitmap bmp = new Bitmap(openFileDialog.FileName))
{
// Process the image
AnyBitmap anyBitmap = AnyBitmap.FromBitmap(bmp);
// Configure barcode reader options (customize as needed)
var option = new BarcodeReaderOptions
{
Speed = ReadingSpeed.Detailed,
ExpectMultipleBarcodes = true,
ScanMode = BarcodeScanMode.Auto,
};
BarcodeResults result = IronBarCode.BarcodeReader.Read(anyBitmap, option);
if (result.Count > 0)
{
string output = string.Empty;
foreach(var barcode in result)
{
Console.WriteLine($"Barcode Found: {barcode.Text}");
output += barcode.Text + "\n";
}
MessageBox.Show($"Detected Barcodes: \n{output}");
}
else
{
MessageBox.Show("No Barcode found.");
}
}
}
catch (Exception ex)
{
MessageBox.Show($"{ex.Message}");
}
}
}
}
}
}
using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MsiInstallerSample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IronSoftware.Logger.LoggingMode = IronSoftware.Logger.LoggingModes.All;
IronSoftware.Logger.LogFilePath = "Default.log";
IronBarCode.License.LicenseKey = "IRONBARCODE-MYLICENSE-KEY-1EF01";
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "Image files (All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
// Load the selected image
using (Bitmap bmp = new Bitmap(openFileDialog.FileName))
{
// Process the image
AnyBitmap anyBitmap = AnyBitmap.FromBitmap(bmp);
// Configure barcode reader options (customize as needed)
var option = new BarcodeReaderOptions
{
Speed = ReadingSpeed.Detailed,
ExpectMultipleBarcodes = true,
ScanMode = BarcodeScanMode.Auto,
};
BarcodeResults result = IronBarCode.BarcodeReader.Read(anyBitmap, option);
if (result.Count > 0)
{
string output = string.Empty;
foreach(var barcode in result)
{
Console.WriteLine($"Barcode Found: {barcode.Text}");
output += barcode.Text + "\n";
}
MessageBox.Show($"Detected Barcodes: \n{output}");
}
else
{
MessageBox.Show("No Barcode found.");
}
}
}
catch (Exception ex)
{
MessageBox.Show($"{ex.Message}");
}
}
}
}
}
}
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Namespace MsiInstallerSample
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
IronSoftware.Logger.LoggingMode = IronSoftware.Logger.LoggingModes.All
IronSoftware.Logger.LogFilePath = "Default.log"
IronBarCode.License.LicenseKey = "IRONBARCODE-MYLICENSE-KEY-1EF01"
Using openFileDialog As New OpenFileDialog()
openFileDialog.Filter = "Image files (All files (*.*)|*.*"
If openFileDialog.ShowDialog() = DialogResult.OK Then
Try
' Load the selected image
Using bmp As New Bitmap(openFileDialog.FileName)
' Process the image
Dim anyBitmap As AnyBitmap = AnyBitmap.FromBitmap(bmp)
' Configure barcode reader options (customize as needed)
Dim option As New BarcodeReaderOptions With {
.Speed = ReadingSpeed.Detailed,
.ExpectMultipleBarcodes = True,
.ScanMode = BarcodeScanMode.Auto
}
Dim result As BarcodeResults = IronBarCode.BarcodeReader.Read(anyBitmap, option)
If result.Count > 0 Then
Dim output As String = String.Empty
For Each barcode In result
Console.WriteLine($"Barcode Found: {barcode.Text}")
output += barcode.Text & vbCrLf
Next
MessageBox.Show($"Detected Barcodes: {vbCrLf}{output}")
Else
MessageBox.Show("No Barcode found.")
End If
End Using
Catch ex As Exception
MessageBox.Show($"{ex.Message}")
End Try
End If
End Using
End Sub
End Class
End Namespace
BarCode讀取邏輯的主要組成部分有哪些?
程式碼使用 OpenFileDialog 來選取圖片,透過 BarcodeReaderOptions 進行詳細掃描,並透過 MessageBox 顯示結果。 閱讀速度選項可根據您的效能需求進行調整。 ExpectMultipleBarcodes 設定可讓系統從單一影像中讀取多個 BARCODE。
如何處理生產環境中的錯誤?
請加入適當的錯誤記錄機制,並提供使用者友善的錯誤訊息,而非直接顯示原始的例外狀況詳細資訊。 請考慮針對BARCODE無法辨識的情況實作重試邏輯,並向使用者提供關於影像品質要求的指引。
我可以用這段程式碼掃描 PDF 文件嗎?
針對 PDF 文件,請將 BarcodeReader.Read 方法替換為 BarcodeReader.ReadPdf,以正確處理 PDF BarCode 擷取。 您亦可探索 PDF 專用的 BarCode 讀取設定,以獲得最佳效能。
以下是修改程式碼以支援 PDF 的範例:
// For PDF documents, use ReadPdf method
if (Path.GetExtension(openFileDialog.FileName).ToLower() == ".pdf")
{
var pdfResults = BarcodeReader.ReadPdf(openFileDialog.FileName, option);
// Process PDF results similar to image results
}
// For PDF documents, use ReadPdf method
if (Path.GetExtension(openFileDialog.FileName).ToLower() == ".pdf")
{
var pdfResults = BarcodeReader.ReadPdf(openFileDialog.FileName, option);
// Process PDF results similar to image results
}
' For PDF documents, use ReadPdf method
If Path.GetExtension(openFileDialog.FileName).ToLower() = ".pdf" Then
Dim pdfResults = BarcodeReader.ReadPdf(openFileDialog.FileName, option)
' Process PDF results similar to image results
End If
如何新增安裝專案來建立 MSI 檔案?
在設定表單及其控制器邏輯後,請將"Setup Project"新增至現有解決方案中,以建立 MSI 安裝程式。 "Setup Project"可讓您為剛建立的應用程式建立安裝程式。 此流程會將所有必要元件(包括 IronBarcode 的依賴項)打包成單一可部署單元。
右鍵點擊解決方案,然後前往"新增">"新專案..."

針對 MSI 安裝程式,請再次以 Release 模式建置 MsiInstallerSample 專案。
在"Setup Project"上按右鍵,然後前往"新增 > 專案輸出..."

為確保 MSI 安裝程式能順利執行,您必須在安裝專案中包含以下三個檔案:IronBarcodeInterop.dll 以及 ReaderInterop.dll。 這些檔案是在您以 Release 模式建置專案時所產生的:
onnxruntime.dll:位於MsiInstallerSample\MsiInstallerSample\bin\ReleaseIronBarcodeInterop.dll:位於MsiInstallerSample\MsiInstallerSample\bin\Release\runtimes\win-x86\nativeReaderInterop.dll:位於MsiInstallerSample\MsiInstallerSample\bin\Release\runtimes\win-x86\native

若缺少上述任何檔案,您可能會遇到以下例外訊息,詳見此疑難排解文章:建立 MSI 安裝程式時缺少 DLL 檔案
最後,建立安裝專案。 安裝程式將位於:MsiInstallerSample\SetupProject\Release
為何需要這些特定的 DLL 檔案?
IronBarcode 仰賴這些原生函式庫,以實現基於機器學習的條碼偵測與處理功能。 onnxruntime.dll 提供機器學習執行環境,而 IronBarcodeInterop.dll 與 ReaderInterop.dll 則負責處理原生 BarCode 處理作業。 這些元件可實現影像校正與容錯等進階功能。
如果我忘記包含這些 DLL 會怎麼樣?
若缺少 DLL 檔案,使用者在安裝後嘗試掃描 BARCODE 時將引發執行時異常。 應用程式可能無法正確初始化 IronBarcode,導致發生 FileNotFoundException 或 DllNotFoundException 錯誤。
如何設定專案的輸出設定?
在新增專案輸出時,請選取"主要輸出"以包含主要可執行檔及其受管依賴項。 此設定可確保所有 .NET 組件均已包含,但請記得手動加入上述提及的原生 DLL 檔案。
如何執行並測試 MSI 安裝程式?
請使用 MSI 檔案安裝應用程式,以確保一切運作順暢。 測試應涵蓋安裝與解除安裝兩種情境,以驗證部署是否正確。

測試時應檢查哪些事項?
請確認應用程式能正確啟動、可開啟檔案對話方塊,並能成功掃描測試圖片中的BARCODE。 請測試各種受支援的BarCode格式,以確保功能完備。 此外,請確認授權金鑰應用程式在部署環境中運作正常。
如何排除安裝問題?
啟用 Windows Installer 記錄功能,以擷取有關安裝失敗或缺少元件的詳細資訊。 請查看 Windows 事件檢視器以獲取更多錯誤詳細資訊,並參閱常見部署問題的疑難排解指南。
我該去哪裡下載完整的範例專案?
您可以下載本指南的完整程式碼。該檔案以 ZIP 形式提供,您可在 Visual Studio 中將其開啟為 WinFormApp 專案。 此範例包含所有必要的設定,並展示 MSI 部署的最佳實務。
範例專案包含哪些內容?
下載內容包含一個完整的 Visual Studio 解決方案,其中包含 Windows Forms 應用程式及已設定的安裝專案。 內容包含用於測試的BarCode範例圖像,並展示部署情境下正確的授權金鑰設定方式。
如何開啟已下載的專案?
請解壓縮 ZIP 檔案,並在已安裝 Installer Projects 擴充套件的 Visual Studio 2022 中開啟 .sln 檔案。 請確保您已透過 NuGet 套件管理員安裝最新版本的 IronBarcode。 如需更詳細的設定說明,請參閱 API 文件。
常見問題
創建條碼應用程序的MSI安裝程序時需要哪些DLL檔案?
在使用IronBarCode創建MSI安裝程序時,您需要包含三個必要的DLL檔案:onnxruntime.dll、IronBarcodeInterop.dll和ReaderInterop.dll。這些檔案確保IronBarCode在您的應用程序透過MSI包部署時能正常運作。
在為我的條碼應用程序創建MSI安裝程序之前,我需要什麼先決條件?
在使用IronBarCode創建MSI安裝程序之前,您需要為Visual Studio 2022下載並安裝Microsoft Visual Studio Installer Projects擴充功能。此外,確保透過NuGet包管理器在專案中安裝了IronBarCode。
如何在C#中快速生成和讀取MSI條碼?
IronBarCode提供一個簡單的API來生成和讀取MSI條碼。您可以使用BarcodeWriter.CreateBarcode()創建一個使用MSI編碼類型的MSI條碼,將其儲存為影像,然後使用BarcodeReader.Read()讀取,並在讀取選項中指定BarcodeEncoding.MSI。
使用MSI安裝程序分發條碼應用程序有哪些優勢?
MSI安裝程序提供標準化的安裝方法,非常適合企業部署。當與IronBarCode結合使用時,它們提供回滾能力、管理安裝點、Windows Installer服務整合,並確保在各種環境中可靠安裝,同時允許開發人員選擇要包括的元件。
條碼程式庫是否在MSI包中支援多種條碼格式?
是的,IronBarCode支援多種條碼格式,使其能應用於各種商用應用程序。這種靈活性允許開發人員根據特定業務需求將不同的條碼類型整合到其MSI包裝的應用程式中。
使用IronBarcode進行條碼操作有什麼好處?
IronBarcode提供了如易於整合、支持多種條碼格式、高品質圖像生成和強大讀取能力等好處,使其成為C#中條碼操作的全面工具。
IronBarcode是否提供自定義條碼外觀的支持?
是的,IronBarcode提供了廣泛的條碼外觀自定義選項,包括顏色、大小和文字註釋,讓您可以根據具體設計需求定制條碼。
IronBarcode如何幫助改善業務流程效率?
IronBarcode通過使條碼生成和讀取快速且準確來提高業務流程效率,減少手動數據輸入錯誤,並改善庫存和資產追蹤。
將IronBarcode實現於專案中需要什麼程式設計技能?
基本的C#程式設計知識足以將IronBarcode實現於專案中,因為它提供了簡單的方法和全面的文檔來指導開發者。
IronBarcode適合於小型專案和大型企業應用嗎?
IronBarcode設計為可擴展且多功能,使其適合小型專案和需要強大條碼解決方案的大型企業應用。

