共用方式為


逐步解說:使用設計工具和 ClickOnce 部署 API 按需下載衛星組件

Windows Forms 應用程式可以透過使用衛星組件來設定多個文化特性。 附屬組件是包含非應用程式預設文化特性之應用程式資源的組件。

在地化 ClickOnce 應用程式中所述,您可以在相同的 ClickOnce 部署中包含多個文化區域的多個附屬組件。 根據預設,ClickOnce 會將部署中的所有附屬組件下載至用戶端電腦,不過單一用戶端可能只需要一個附屬組件。

本逐步解說示範如何將附屬組件標示為可選,以及僅下載用戶端電腦依其目前文化設定所需的組件。

備註

ApplicationDeployment命名空間中的System.Deployment.Application類別和 API 不支援 .NET Core 和 .NET 5 和更新版本。 在 .NET 7 中,支援存取應用程式部署屬性的新方法。 如需詳細資訊,請參閱 存取 .NET 中的 ClickOnce 部署屬性。 .NET 7 不支援對等的 ApplicationDeployment 方法。

備註

基於測試目的,下列程式碼範例以程式設計方式將文化特性設定為 ja-JP。 請參閱本主題稍後的「後續步驟」一節,以取得如何針對生產環境調整此程式碼的資訊。

將衛星元件標示為選用

  1. 建立您的專案。 這將為您本地化到的所有文化/地區生成衛星組件。

  2. 以滑鼠右鍵按一下 [方案總管] 中的專案名稱,然後按一下 [屬性]。

  3. 按一下 [發佈] 索引標籤,然後按一下 [應用程式檔案]。

  4. 選取「 顯示所有檔案 」核取方塊以顯示附屬組件。 根據預設,所有衛星組件都會包含在您的部署中,並在此對話方塊中顯示。

    附屬元件的名稱會採用 isoCode<\ApplicationName.resources.dll格式>,其中 <isoCode> 是 RFC 1766 格式的語言識別碼。

  5. 按一下每個語言識別碼的下載群組清單中的新增。 當系統提示您輸入下載群組名稱時,請輸入語言識別碼。 例如,對於日文衛星組件,您可以指定下載群組名稱 ja-JP

  6. 關閉 [應用程式檔案] 對話方塊。

在 C# 中按需下載衛星組件

  1. 開啟 Program.cs 檔案。 如果您在 [方案總管] 中看不到此檔案,請選取您的專案,然後在 [ 專案 ] 功能表上,按一下 [顯示所有檔案]。

  2. 使用以下程式碼下載適當的衛星組件並啟動您的應用程式。

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Threading;
    using System.Globalization;
    using System.Deployment.Application;
    using System.Reflection;
    
    namespace ClickOnce.SatelliteAssemblies
    {
        static class Program
        {
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("ja-JP");
    
                // Call this before initializing the main form, which will cause the resource manager
                // to look for the appropriate satellite assembly.
                GetSatelliteAssemblies(Thread.CurrentThread.CurrentCulture.ToString());
    
                Application.Run(new Form1());
            }
    
            static void GetSatelliteAssemblies(string groupName)
            {
                if (ApplicationDeployment.IsNetworkDeployed)
                {
                    ApplicationDeployment deploy = ApplicationDeployment.CurrentDeployment;
    
                    if (deploy.IsFirstRun)
                    {
                        try
                        {
                            deploy.DownloadFileGroup(groupName);
                        }
                        catch (DeploymentException de)
                        {
                            // Log error. Do not report this error to the user, because a satellite
                            // assembly may not exist if the user's culture and the application's
                            // default culture match.
                        }
                    }
                }
            }
    
        }
    }
    

在 Visual Basic 中按需下載附屬元件

  1. 在應用程式的 「內容」 視窗中,按一下 「應用程式 」標籤。

  2. 在標籤頁底部,按一下 檢視應用程式事件

  3. 將下列匯入新增至 ApplicationEvents.VB 檔案的開頭。

    Imports System.Deployment.Application
    Imports System.Globalization
    Imports System.Threading
    
  4. 將下列程式碼 MyApplication 新增至類別。

    Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
        Thread.CurrentThread.CurrentUICulture = New CultureInfo("ja-JP")
        GetSatelliteAssemblies(Thread.CurrentThread.CurrentUICulture.ToString())
    End Sub
    
    Private Shared Sub GetSatelliteAssemblies(ByVal groupName As String)
        If (ApplicationDeployment.IsNetworkDeployed) Then
    
            Dim deploy As ApplicationDeployment = ApplicationDeployment.CurrentDeployment
    
            If (deploy.IsFirstRun) Then
                Try
                    deploy.DownloadFileGroup(groupName)
                Catch de As DeploymentException
                    ' Log error. Do not report this error to the user, because a satellite
                    ' assembly may not exist if the user's culture and the application's
                    ' default culture match.
                End Try
            End If
        End If
    End Sub
    

後續步驟

在生產環境中,您可能需要移除程式碼範例中設定 CurrentUICulture 為特定值的行,因為用戶端電腦預設會設定正確的值。 例如,當您的應用程式在日文用戶端電腦上執行時,CurrentUICulture預設會為。ja-JP 以程式設計方式設定它是在部署應用程式之前測試附屬元件的好方法。