逐步解說:使用設計工具依 ClickOnce 部署 API 的要求下載附屬組件

透過使用附屬組件,Windows Forms 應用程式可以設定為適用多個文化特性。 「附屬組件」 (Satellite Assembly) 為包含文化特性 (除了應用程式的預設文化特性以外) 之應用程式資源的組件。

當地語系化 ClickOnce 應用程式中所述,您可以在相同的 ClickOnce 部署中包含多個附屬組件,以因應多個文化特性。 儘管單一用戶可能只需要一個附屬組件,但 ClickOnce 預設會將您部署中所有的附屬組件下載到用戶端電腦。

本逐步解說示範如何標示您的附屬組件為選擇性,並僅下載用戶端電腦目前文化特性所需要的附屬組件。

注意

.NET Core 和 .NET 5 和更新版本中不支援 System.Deployment.Application 命名空間中的 ApplicationDeployment 類別和 API。 .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 。 在部署應用程式前,以程式設計方式設定這個值是測試附屬組件的一個好方法。