使用 IronBarCode 建立 C# MSI 安裝程序

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

MSI(Microsoft Installer)是一種 Windows 安裝包,可輕鬆管理軟體的安裝、更新和卸載。 使用 MSI 提供了一種標準化的應用程式安裝方法,這對於企業級部署尤其有利。

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

本教學將簡要示範如何從範例條碼應用程式建立 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

立即開始在您的項目中使用 IronBarcode 並免費試用。

第一步:
green arrow pointer

先決條件

在開始專案之前,請下載[Microsoft Visual Studio Installer Projects 擴充功能]( 要讓 MSI 建置正常運作,需要從 https://marketplace.visualstudio.com/items?itemName=VisualStudioClient.MicrosoftVisualStudio2022InstallerProjects 取得該專案。

建立 MSI 安裝程式

在本範例中,我們將使用 Windows Forms 應用程式 (.NET Framework) 專案來示範其功能。

新增按鈕

  • 導航至工具箱 搜尋按鈕
  • 將按鈕拖曳到視窗窗體上。

新增按鈕

編輯按鈕程式碼

雙擊按鈕元件以存取窗體的 C# 程式碼。 以下是表單元件的邏輯,它接收條碼並嘗試掃描它。 此程式碼僅掃描影像,不適用於 PDF 檔案。 對 PDF 文件使用 ReadPdf 方法。

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}");
                    }
                }
            }
        }
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

新增安裝項目

設定好表單及其控制器邏輯後,我們需要為現有解決方案新增一個安裝項目來建立 MSI 安裝程式。 安裝專案允許我們為剛剛建立的應用程式建立安裝程式。

右鍵單擊解決方案,然後選擇"新增" > "新專案..."

新增安裝項目

對於 MSI 安裝程序,請在 Release 模式下再次建置 MsiInstallerSample 專案。 右鍵單擊"設定項目",然後選擇"新增" > "項目輸出…"

新增項目輸出

為確保 MSI 安裝程式順利運行,您必須在安裝專案中包含以下三個檔案: onnxruntime.dllIronBarcodeInterop.dllReaderInterop.dll 。 這些文件是在以發布模式建立專案時產生的:

  • onnxruntime.dll :位於 MsiInstallerSample\MsiInstallerSample\bin\Release
  • IronBarcodeInterop.dll :位於 MsiInstallerSample\MsiInstallerSample\bin\Release\runtimes\win-x86\native
  • ReaderInterop.dll :位於 MsiInstallerSample\MsiInstallerSample\bin\Release\runtimes\win-x86\native

新增其他 DLL 文件

如果缺少任何這些文件,您可能會遇到以下異常訊息,如這篇故障排除文章所述:在建立 MSI 安裝程式時缺少 DLL 文件

最後,建置安裝專案。 安裝程式位於:MsiInstallerSample\SetupProject\Release

運行並測試安裝程序

我們使用 MSI 檔案安裝應用程序,以確保一切運作順暢。

Demonstration related to 運行並測試安裝程序

下載 MSI 安裝程式範例項目

您可以下載本指南的完整程式碼。程式碼以壓縮檔案的形式提供,您可以在 Visual Studio 中將其作為 WinFormApp 專案開啟。

下載 WinForm MSI 應用程式項目

常見問題解答

什麼是 IronBarCode?

IronBarCode 是一個允許開發人員在 .NET 應用程式中讀取、寫入和生成條碼的庫。它支持多種條碼格式,設計易於集成。

如何使用 IronBarCode 創建 MSI 安裝程式?

要創建與 IronBarCode 集成的 MSI 安裝程式,您需要將 IronBarCode 庫集成到您的應用程式中,並遵循使用諸如 Visual Studio 安裝專案等工具創建 MSI 安裝程式的標準步驟。

在 MSI 安裝程式中使用 IronBarCode 有什麼好處?

在 MSI 安裝程式中集成 IronBarCode 可以簡化需要條碼功能的應用程式的部署過程,確保所有必要的組件均正確安裝在最終使用者系統中。

IronBarCode 支持哪些條碼格式?

IronBarCode 支持多種條碼格式,包括 QR 碼、UPC、EAN、Code 39、Code 128 等,能夠滿足不同應用程式的需求。

IronBarCode 可以自定義條碼外觀嗎?

可以,IronBarCode 允許自定義條碼外觀,包括顏色、尺寸和文本註釋,以適應應用程式的特定設計要求。

將 IronBarCode 集成到 MSI 安裝程式是否需要特定工具?

您可以使用常用的開發工具,例如 Visual Studio,將 IronBarCode 集成到您的應用程式並創建 MSI 安裝程式。IronBarCode 庫與 .NET 相容,方便使用此框架的開發人員訪問。

IronBarCode 能讀取和寫入二維條碼嗎?

可以,IronBarCode 能讀取和寫入一維和二維條碼,包括 QR 碼、數據矩陣等,提供全面的條碼功能。

使用 IronBarCode 的系統要求是什麼?

IronBarCode 需要一個 .NET 框架環境運行。它與 .NET Core、.NET 5/6 及更早版本相容,確保在不同項目中具有廣泛的相容性。

如何開始使用 IronBarCode?

若要開始使用 IronBarCode,您可以從 Iron Software 網站下載該庫,查看文件,並按照提供的範例和指南將其集成到您的 .NET 項目中。

IronBarCode 使用者是否有技術支援可用?

有,Iron Software 為 IronBarCode 使用者提供技術支援,包括文檔、教程和直接支援選項,以協助集成及使用的問題。

Curtis Chau
技術作家

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

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

準備好開始了嗎?
Nuget 下載 1,979,979 | Version: 2025.11 剛發表