使用IronBarcode建立C# MSI安裝程式

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

要使用IronBarcode建立MSI安裝程式,將Setup Project新增到您的方案中,包含所需的DLL(ReaderInterop.dll),並構建該專案以生成可分發的MSI套件。

MSI(Microsoft Installer)是一種Windows安裝包,能夠促進軟體安裝、更新和移除。 使用MSI提供了一種標準化的方法來安裝應用程式,這對於企業部署特別有利。 MSI格式支援高級功能,例如回滾能力、管理安裝點和Windows Installer服務整合。

IronBarcode提供工具,可與您現有的應用程式無縫整合,並將其轉換為MSI,以便於分發。 它可確保在各種環境中的可靠安裝,並允許開發者選擇要包含或排除的組件。 該程式庫支援多種條碼格式,使其對於各種業務應用程式具有多功能性。

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

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

使用IronBarcode的簡單API即可輕鬆建立和讀取MSI條碼,設置最低。以下程式碼片段顯示了如何輕鬆寫入MSI條碼圖像,然後讀取它——全部只需幾行程式碼。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/BarCode

    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

開始之前需要哪些先決條件?

在開始專案之前,請下載 [Microsoft Visual Studio Installer Projects擴展]
https://marketplace.visualstudio.com/items?itemName=VisualStudioClient.MicrosoftVisualStudio2022InstallerProjects)以使MSI構建正常運行。 此外,請確保您已在專案中通過NuGet安裝IronBarcode。

為何需要Visual Studio Installer Projects擴展?

該擴展提供了Visual Studio 2022中建立MSI安裝程式所需的Setup Project模板。此擴展新增了部署專案模板,這些模板在Visual Studio 2010版本之後被移除,使開發者能夠建立傳統Windows安裝程式包。

我應該針對哪個版本的.NET Framework?

使用Windows Forms App(.NET Framework)以獲得與MSI部署場景的最大相容性。 雖然IronBarcode支援各種.NET平台,但.NET Framework版本確保了在通常部署MSI安裝程式的Windows系統上的最廣泛相容性。

我如何建立初始的MSI安裝程式專案?

在此範例中,使用Windows Forms App(.NET Framework)專案來演示其功能。 這種方法為桌面條碼掃描應用程式提供了一種熟悉的UI範式。

哪種型別的專案最適合作為MSI安裝程式?

Windows Forms應用程式為建立MSI部署的條碼掃描應用程式提供了最直接的途徑。 它們提供了與Windows的本機整合,不需要超出.NET Framework的其他運行時依賴項。

演示應用程式的關鍵組件是什麼?

該應用程式由一個帶有按鈕的表單組成,該按鈕打開文件對話框以從圖像中掃描條碼。 此簡單介面展示了核心功能,同時保持部署的複雜性最小化。 對於更高級的場景,請考慮探索從PDF中讀取條碼或實現異步條碼讀取

如何將按鈕新增到Windows Form?

  • 導航至ToolBox
  • 搜索Button
  • 通過拖放將按鈕新增到Windows表單上

Visual Studio工具箱顯示高亮顯示的按鈕控制項在"所有Windows表單"部分可新增到表單中

在哪可以找到按鈕控件?

在Visual Studio工具箱的"常用控件"部分可以找到Button控件。 如果工具箱不可見,請從View > Toolbox打開它,或者按Ctrl+Alt+X。

如何在表單上定位按鈕?

將按鈕置於表單的中央位置或使用者自然期望找到主要操作的地方。 考慮遵循Windows UI指南,以保持在各應用程式中的一致性使用者體驗。

我如何編輯按鈕程式碼以處理條碼掃描?

雙擊按鈕元件以存取表單的C#程式碼。 以下是表單元件的邏輯——它接收條碼並嘗試進行掃描。 此程式碼僅掃描圖像,不適用於PDF。 使用ReadPdf方法以處理PDF文件。 有關全面的條碼讀取選項,請參閱條碼讀取器設置文件

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
$vbLabelText   $csharpLabel

條碼讀取邏輯的關鍵組件是什麼?

該程式碼使用MessageBox顯示結果。 讀取速度選項可以根據您的效能需求進行調整。 ExpectMultipleBarcodes設置允許從單個圖像中讀取多個條碼

我應該如何處理生產中的錯誤?

包括適當的錯誤日誌記錄和使用者友好的錯誤訊息,而不是顯示原始的異常詳細資訊。 考慮實現檢查邏輯,例如條碼未被識別場景,並為使用者提供圖像質量要求的指導。

這段程式碼能掃描PDF文件嗎?

對於PDF文件,將BarcodeReader.ReadPdf,以正確處理PDF條碼提取。 您也可以探索PDF特定條碼讀取器設置以獲得優化效能。

以下是一個如何修改程式碼以支持PDF的範例:

:path=/static-assets/barcode/content-code-examples/how-to/msi-installer-3.cs
// For PDF documents, use ReadPdf method
if (Path.GetExtension(openFileDialog.FileName).ToLower() == ".pdf")
{
    var pdfOption = new PdfBarcodeReaderOptions
    {
        Speed = ReadingSpeed.Detailed,
        ExpectMultipleBarcodes = true,
    };
    var pdfResults = BarcodeReader.ReadPdf(openFileDialog.FileName, pdfOption);
    // Process PDF results similar to image results
}
Imports System.IO

' For PDF documents, use ReadPdf method
If Path.GetExtension(openFileDialog.FileName).ToLower() = ".pdf" Then
    Dim pdfOption As New PdfBarcodeReaderOptions With {
        .Speed = ReadingSpeed.Detailed,
        .ExpectMultipleBarcodes = True
    }
    Dim pdfResults = BarcodeReader.ReadPdf(openFileDialog.FileName, pdfOption)
    ' Process PDF results similar to image results
End If
$vbLabelText   $csharpLabel

如何新增設置專案以建立MSI?

在設置表單及其控制邏輯後,將設置專案新增到現有解決方案以建立MSI安裝程式。 設置專案允許您為剛剛建立的應用程式構建安裝程式。 此過程會將所有必要的組件打包到一個可部署的單位中,包括IronBarcode依賴項。

右鍵點擊解決方案,然後前往新增 > 新專案...

Visual Studio解決方案管理器上下文選單顯示新增>新專案選項以建立安裝設置專案

對於MSI安裝程式,再次在Release模式下構建MsiInstallerSample專案。
右鍵點擊設置專案,然後前往新增 > 專案輸出...

Visual Studio新增專案輸出組對話框顯示設置專案的部署選項,解決方案瀏覽器可見

為確保MSI安裝程式順利運行,您必須在您的設置專案中包含以下三個檔案:onnxruntime.dll, IronBarcodeInterop.dll, 和ReaderInterop.dll。 這些檔案是在Release模式下構建專案時生成的:

  • MsiInstallerSample\MsiInstallerSample\bin\Release
  • MsiInstallerSample\MsiInstallerSample\bin\Release\runtimes\win-x86\native
  • MsiInstallerSample\MsiInstallerSample\bin\Release\runtimes\win-x86\native

Visual Studio檔案對話框選擇ReaderInterop.dll以新增到設置專案,解決方案瀏覽器顯示依賴項

如果缺少這些檔案中的任何一個,您可能會遇到以下异常資訊,如此疑難解答文章中所述:建立MSI安裝程式時缺少的DLL

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

為什麼需要這些特定的DLL?

IronBarcode依賴於這些本地程式庫來實現基於ML的條碼檢測和處理能力。 ReaderInterop.dll則處理本地條碼處理操作。 這些組件支持高級功能,如圖像校正容錯

如果我忘記包括這些DLL會怎樣?

缺少的DLL會導致使用者在安裝後嘗試掃描條碼時出現運行時異常。 應用程式可能無法正確初始化IronBarcode,從而導致DllNotFoundException錯誤。

如何配置專案輸出設置?

新增專案輸出時,選擇"主要輸出"以包括主要可執行文件及其托管依賴項。 這確保包含所有.NET組件,但請記得手動新增上述本地DLL。

我如何運行和測試MSI安裝程式?

使用MSI文件安裝應用程式,以確保一切正常運行。 測試應包括安裝和解除安裝場景,以驗證正確的部署。

Visual Studio顯示已完成的安裝程式構建,Release文件夾包含setup.exe和安裝程式檔案

在測試期間我應該檢查什麼?

確認應用程式正確啟動、能夠打開文件對話框以及成功從測試圖像中掃描條碼。 使用多種支援的條碼格式進行測試以確保全面的功能。 還需驗證授權金鑰在部署環境中的正確應用。

我如何排除安裝問題?

啟用Windows安裝程式日誌記錄,以捕獲任何安裝失敗或缺少組件的詳細資訊。 檢查Windows事件查看器以獲取其他錯誤詳情,並參考疑難解答指南以處理常見的部署問題。

我在哪裡可以下載完整的樣本項目?

您可以下載指導使用的完整程式碼。它是壓縮文件形式,您可以在Visual Studio中以WinFormApp項目打開。 該範例包含所有必要的配置並演示MSI部署的最佳實踐。

下載WinForm MSI應用程式專案

範例項目包含什麼內容?

下載內容包括一個完整的Visual Studio解決方案,其中包含Windows Forms應用程式和配置的設置專案。 它包括測試用的範例條碼圖像,並演示了授權金鑰配置的部署場景。

我如何打開下載的專案?

解壓縮ZIP文件並在已安裝Installer Projects擴展的Visual Studio 2022中打開.sln文件。 確保您已通過NuGet包管理器安裝最新版的IronBarcode。 如需更詳細的安裝說明,請參閱API文件

常見問題

建立帶有條碼應用程式的 MSI 安裝程式需要哪些 DLL 文件?

using IronBarCode 建立 MSI 安裝程式時,您需要包含三個必要的 DLL 文件:onnxruntime.dll、IronBarcodeInterop.dll 和 ReaderInterop.dll。這些文件可確保當您的應用程式通過 MSI 套件部署時,IronBarCode 能正常運作。

在為我的條碼應用程式建立 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。

using MSI 安裝程式分發條碼應用程式有哪些優勢?

MSI 安裝程式提供了標準化的安裝方法,非常適合企業部署。結合 IronBarCode 時,它們提供了回滾功能、管理安裝點、Windows 安裝程式服務整合,並確保在各種環境中的可靠安裝,同時允許開發者選擇要包含的組件。

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

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

使用IronBarcode進行條碼操作有什麼好處?

IronBarcode提供了如易於整合、支持多種條碼格式、高品質圖像生成和強大讀取能力等好處,使其成為C#中條碼操作的全面工具。

IronBarcode是否提供自定義條碼外觀的支持?

是的,IronBarcode提供了廣泛的條碼外觀自定義選項,包括顏色、大小和文字註釋,讓您可以根據具體設計需求定制條碼。

IronBarcode如何幫助改善業務流程效率?

IronBarcode通過使條碼生成和讀取快速且準確來提高業務流程效率,減少手動資料輸入錯誤,並改善庫存和資產追蹤。

將IronBarcode實現於專案中需要什麼程式設計技能?

基本的C#程式設計知識足以將IronBarcode實現於專案中,因為它提供了簡單的方法和全面的文件來指導開發者。

IronBarcode適合於小型專案和大型企業應用嗎?

IronBarcode設計為可擴展且多功能,使其適合小型專案和需要強大條碼解決方案的大型企業應用。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備好開始了嗎?
Nuget 下載 2,331,688 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動嗎?

想快速驗證嗎? PM > Install-Package BarCode
運行範例觀看您的字串成為條碼。