次の方法で共有


チュートリアル: デザイナーを使用して ClickOnce 配置 API を使用して必要に応じてサテライト アセンブリをダウンロードする

Windows フォーム アプリケーションは、サテライト アセンブリを使用して複数のカルチャ用に構成できます。 サテライト アセンブリは、アプリケーションの既定のカルチャ以外のカルチャのアプリケーション リソースを含むアセンブリです。

ClickOnce アプリケーションのローカライズで説明したように、同じ ClickOnce 配置内に複数のカルチャの複数のサテライト アセンブリを含めることができます。 既定では、ClickOnce は配置内のすべてのサテライト アセンブリをクライアント コンピューターにダウンロードしますが、1 つのクライアントでは必要なサテライト アセンブリが 1 つだけである可能性があります。

このチュートリアルでは、サテライト アセンブリを省略可能としてマークし、クライアント コンピューターが現在のカルチャ設定に必要なアセンブリのみをダウンロードする方法について説明します。

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 されます。 プログラムによる設定は、アプリケーションをデプロイする前にサテライト アセンブリをテストするのに適した方法です。