使用 IronBarcode 創建 C# MSI 安裝程式。

This article was translated from English: Does it need improvement?
Translated
View the article in English

要使用 IronBarcode 創建 MSI 安裝程式,請在您的解決方案中加入 Setup Project,包含所需的 DLL (onnxruntime.dllIronBarcodeInterop.dllReaderInterop.dll),並建立專案以產生可發佈的 MSI 套件。

MSI (Microsoft Installer) 是一種 Windows 安裝套件,方便軟體安裝、更新與移除。 使用 MSI 可提供安裝應用程式的標準化方法,這對企業部署尤其有利。 MSI 格式支援進階功能,例如回滾功能、管理安裝點以及 Windows Installer 服務整合。

IronBarCode 提供工具,可與您現有的應用程式無縫集成,並將其轉換為 MSI 檔案以便於分發。 它可確保在各種環境下可靠安裝,並允許開發人員選擇要包含或排除的元件。 該函式庫支援 多種條碼格式,使其適用於各種商業應用程式。

本教學示範如何利用 IronBarCode 強大的掃描功能,從一個條碼應用程式範例中建立 MSI 檔案。

快速入門:一鍵產生和讀取 MSI 條碼

使用 IronBarcode 簡單的 API,只需最少的設定即可建立和讀取 MSI 條碼。下面的片段展示了如何輕鬆寫入 MSI 條碼圖像,然後再讀取 - 所有這些都只需要幾行。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronBarcode

    PM > Install-Package BarCode

  2. 複製並運行這段程式碼。

    var msiImg = IronBarCode.BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.MSI).SaveAsImage("msi.png");
    var results = IronBarCode.BarcodeReader.Read("msi.png", new BarcodeReaderOptions { ExpectBarcodeTypes = BarcodeEncoding.MSI });
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronBarcode,免費試用!
    arrow pointer

!{--01001100010010010100001001010010010000010101001001011001010111110101001101010100010001010101010 10100010111110101010001010010010010010100000101001100010111110100001001001100010011111010000100100110001001111010101

開始前我需要哪些先決條件? 在開始專案之前,請下載 [Microsoft Visual Studio Installer Projects extension]( 要讓 MSI 建置正常運作,需要從 https://marketplace.visualstudio.com/items?itemName=VisualStudioClient.MicrosoftVisualStudio2022InstallerProjects 取得該專案。 此外,請確保您已透過 NuGet 在專案中安裝 IronBarcode。

為什麼需要 Visual Studio Installer Projects 擴充套件? 本擴充套件提供在 Visual Studio 2022 中建立 MSI 安裝程式所需的設定專案範本。此擴充套件新增了在 2010 版之後從 Visual Studio 移除的部署專案範本,讓開發人員可以建立傳統的 Windows Installer 套件。

我應該針對哪個版本的 .NET Framework? 使用 Windows Forms App (.NET Framework) 與 MSI 部署情境達到最大相容性。 雖然 IronBarcode 支援 [各種 .NET 平台](https://ironsoftware.com/csharp/barcode/features/compatibility/),但 .NET Framework 版本可確保與通常部署 MSI 安裝程式的 Windows 系統有最廣泛的相容性。

如何建立初始 MSI 安裝程式專案? 在本範例中,使用 Windows Forms App (.NET Framework) 專案來展示其功能。 此方法可為桌上型電腦條碼掃描應用程式提供熟悉的使用者介面範例。

哪種專案類型最適合 MSI 安裝程式? Windows Forms 應用程式為建立 MSI 部署的 BarCode 掃描應用程式提供了最直接的路徑。 這些工具提供本機 Windows 整合,除了 .NET Framework 之外,不需要額外的執行時依賴。

示範應用程式的主要元件為何? 應用程式包含一個表單,表單上的按鈕可開啟檔案對話框,從影像掃描 BarCode。 這個簡單的介面在展示核心功能的同時,也將部署的複雜性降至最低。 對於更進階的情境,請考慮探索 [ 從 PDF 讀取條碼](https://ironsoftware.com/csharp/barcode/how-to/read-barcodes-from-pdf/) 或實作 [async 條碼讀取](https://ironsoftware.com/csharp/barcode/how-to/async-multithread/)。

如何在 Windows 表單中加入按鈕? - 導航至工具箱 搜尋按鈕 - 將按鈕拖放到 Windows 表單上 ![Visual Studio 工具箱顯示在 All Windows Forms 區段下高亮顯示的 Button 控制項,可新增至表格](/static-assets/barcode/how-to/msi-installer/add-button.webp)。

我在哪裡可以找到按鈕控制項? Button 控件位於 Visual Studio 工具箱的常用控件區。 如果工具箱不顯示,請從檢視 > 工具箱開啟,或按 Ctrl+Alt+X。

我應該如何定位表單上的按鈕? 將按鈕置於表單的中心,或置於使用者自然期望找到主要動作的位置。 考慮遵循 Windows UI 指南,以獲得跨應用程式一致的使用者體驗。

如何編輯按鈕代碼以處理條碼掃描? 雙擊按鈕元件可存取表單的 C# 程式碼。 以下是表單元件的邏輯 - 它接收一個 BarCode 並嘗試掃描它。 此代碼僅能掃描影像,對 PDF 無效。 對 PDF 文件使用 `ReadPdf` 方法。 如需全面的條碼讀取選項,請參閱 [條碼讀取器設定文件](https://ironsoftware.com/csharp/barcode/examples/barcode-reader-settings-csharp/)。 ```cs 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}"); } } } } } } ```

Barcode 讀取邏輯的關鍵元件是什麼? 程式碼使用 `OpenFileDialog` 來選擇影像,設定 `BarcodeReaderOptions` 以進行詳細掃描,並透過 `MessageBox` 來顯示結果。 [閱讀速度選項](https://ironsoftware.com/csharp/barcode/how-to/reading-speed-options/)可根據您的效能需求調整。 `ExpectMultipleBarcodes` 設定可讓 [ 從單一影像讀取多個條碼](https://ironsoftware.com/csharp/barcode/how-to/read-multiple-barcodes/)。

我應該如何處理生產中的錯誤? 包括適當的錯誤記錄和使用者友善的錯誤訊息,而非顯示原始的異常詳細資訊。 考慮針對[條碼未識別](https://ironsoftware.com/csharp/barcode/troubleshooting/barcode-not-recognized/)的情況實施重試邏輯,並就影像品質要求向使用者提供指引。

我可以使用此代碼掃描 PDF 文件嗎? 對於 PDF 文件,請用 `BarcodeReader.Read` 方法取代 `BarcodeReader.ReadPdf` 方法,以正確處理 PDF 條碼擷取。 您也可以探索[特定於 PDF 的 BarCode 閱讀器設定](https://ironsoftware.com/csharp/barcode/examples/pdf-barcode-reader-settings-csharp/),以獲得最佳化的效能。 以下是如何修改程式碼以支援 PDF 的範例: ```cs // 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 } ```

如何新增設定專案以建立 MSI? 設定表單及其控制器邏輯後,新增一個 Setup 專案到現有的解決方案,以建立 MSI 安裝程式。 Setup 專案可讓您為剛建立的應用程式建立安裝程式。 此流程將所有必要的元件,包括 IronBarcCode 的相依性,打包成單一的可部署單元。 右鍵單擊解決方案,然後選擇**"新增"** > **"新專案..."** ![Visual Studio Solution Explorer 上下文功能表顯示新增 > 新增專案選項,用於建立設定專案](/static-assets/barCode/how-to/msi-installer/add-setup-project.webp)。 對於 MSI 安裝程序,請在 Release 模式下再次建置 MsiInstallerSample 專案。 右鍵單擊"設定項目",然後選擇**"新增"** > **"項目輸出…"** ![Visual Studio 新增專案輸出群組對話框,顯示 SetupProject 的部署選項,且 Solution Explorer 可見](/static-assets/barCode/how-to/msi-installer/add-project-output.webp)。 為確保 MSI 安裝程式順利運行,您必須在安裝專案中包含以下三個檔案: `onnxruntime.dll` 、 `IronBarcodeInterop.dll`和`ReaderInterop.dll` 。 這些文件是在以發布模式建立專案時產生的: - `onnxruntime.dll`:位於`MsiInstallerSample\MsiInstallerSample\bin\Release` - `IronBarcodeInterop.dll`:位於 `MsiInstallerSample\MsiInstallerSample\bin/Release\runtimes\win-x86\native`. - `ReaderInterop.dll`:位於 `MsiInstallerSample\MsiInstallerSample\binRelease\runtimes\win-x86\native` 中。 ![Visual Studio 檔案對話框選擇 ReaderInterop.dll 以新增至 setup 專案,Solution Explorer 顯示相依性](/static-assets/BarCode/how-to/msi-installer/add-additional-dll.webp)。 如果缺少這些檔案中的任何一個,您可能會遇到下列異常訊息,如本疑難排解文章所述:[在建立 MSI 安裝程式時遺失 DLL](https://ironsoftware.com/csharp/barcode/troubleshooting/missing-dll-msi-installer/) 最後,建置安裝專案。 安裝程式將位於`MsiInstallerSample\SetupProject\Release`

為什麼需要這些特定的 DLL? IronBarcode 依賴這些原生函式庫來實現基於 ML 的條碼檢測和處理功能。 onnxruntime.dll 提供機器學習運行時間,而 IronBarcodeInterop.dll 和 ReaderInterop.dll 則處理原生條碼處理作業。 這些元件可實現 [ 影像修正](https://ironsoftware.com/csharp/barcode/how-to/image-correction/) 和 [ 容錯](https://ironsoftware.com/csharp/barcode/features/fault-tolerance/) 等進階功能。

如果我忘了包含這些 DLLs 會怎樣? 當使用者在安裝後嘗試掃描 BarCode 時,若 DLL 遺失會造成執行時異常。 應用程式可能無法正確初始化 IronBarcode,導致 FileNotFoundException 或 DllNotFoundException 錯誤。

我要如何設定專案輸出設定? 新增專案輸出時,請選擇"主要輸出",以包含主可執行檔及其管理的相依性。 這可確保包含所有 .NET 程式集,但請記得手動加入上述的本機 DLL。

如何執行和測試 MSI 安裝程式? 使用 MSI 檔案安裝應用程式,以確保一切運作順利。 測試應涵蓋安裝和卸載兩種情況,以驗證部署是否正確。 ![Visual Studio 顯示已完成的安裝程式建立,包含 setup.exe 和安裝程式檔案的 Release 資料夾](/static-assets/barcode/how-to/msi-installer/demonstration.gif)。

測試期間我應該檢查什麼? 驗證應用程式是否能正確啟動、開啟檔案對話方塊,以及成功掃描測試影像中的 BarCode。 使用各種 [ 支援的 BarCode 格式](https://ironsoftware.com/csharp/barcode/get-started/supported-barcode-formats/)進行測試,以確保功能全面。 此外,還要驗證 [ 授權金鑰應用程式](https://ironsoftware.com/csharp/barcode/troubleshooting/apply-a-license-key-in-ironbarcode/)是否能在部署的環境中正常運作。

如何排除安裝問題? 啟用 Windows 安裝程式記錄,以擷取任何安裝失敗或元件遺失的詳細資訊。 檢查 Windows 事件檢視器以瞭解其他錯誤細節,並參閱 [ 疑難排解指南](https://ironsoftware.com/csharp/barcode/troubleshooting/) 以瞭解常見的部署問題。

哪裡可以下載完整的範例專案? 您可以下載本指南的完整程式碼。程式碼以壓縮檔案的形式提供,您可以在 Visual Studio 中將其作為 WinFormApp 專案開啟。 範例包含所有必要的設定,並示範 MSI 部署的最佳實務。 [下載 WinForm MSI 應用程式項目](/static-assets/barcode/how-to/msi-installer/MsiInstallerSample.zip)

範例專案包含哪些內容? 下載內容包含完整的 Visual Studio 解決方案,其中包含 Windows Forms 應用程式和已設定的 Setup Project。 它包含用於測試的示範 BarCode 影像,並示範適當的[許可金鑰配置](https://ironsoftware.com/csharp/barcode/get-started/license-keys/),以應用於部署情境。

如何開啟下載的專案? 解壓縮 ZIP 檔案,並在安裝安裝程式專案擴充套件的 Visual Studio 2022 中開啟 `.sln` 檔案。 確保您已透過 NuGet 套件管理員安裝最新版本的 IronBarcode。 如需更詳細的設定說明,請參閱 [API 文件](https://ironsoftware.com/csharp/barcode/object-reference/api/)。

常見問題解答

使用 BarCode 應用程式建立 MSI 安裝程式時,需要哪些 DLL 檔案?

在使用 IronBarcode 創建 MSI 安裝程式時,您需要包含三個重要的 DLL 檔案:onnxruntime.dll、IronBarcodeInterop.dll 和 ReaderInterop.dll。這些檔案可確保當您的應用程式透過 MSI 套件部署時,IronBarcode 能正常運作。

在為我的 BarCode 應用程式建立 MSI 安裝程式之前,我需要哪些先決條件?

在使用 IronBarcode 建立 MSI 安裝程式之前,您需要下載並安裝 Visual Studio 2022 的 Microsoft Visual Studio Installer Projects 擴充套件。此外,確保 IronBarCode 已經透過 NuGet 套件管理程式安裝在您的專案中。

如何在 C# 中快速生成和讀取 MSI BarCode?

IronBarCode 提供一個簡單的 API 來產生和讀取 MSI 條碼。您可以使用BarcodeWriter.CreateBarcode()以MSI編碼類型創建一個MSI條碼,將其保存為圖像,然後在讀取器選項中指定BarcodeEncoding.MSI,使用BarcodeReader.Read()將其讀回。

使用 MSI 安裝程式發佈 BarCode 應用程式有哪些優點?

MSI 安裝程式提供標準化的安裝方法,是企業部署的理想選擇。當與 IronBarcode 結合時,它們可提供回滾功能、管理安裝點、Windows Installer 服務整合,並確保在各種環境中進行可靠的安裝,同時允許開發人員選擇包含哪些元件。

條碼庫是否支援 MSI 套件中的多種條碼格式?

是的,IronBarCode 支援多種條碼格式,使其適用於各種商業應用程式。這種靈活性允許開發人員根據特定的業務需求,將不同的條碼類型整合到他們的 MSI 套件應用程式中。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 2,002,059 | 版本: 2025.12 剛發表