.NET アップグレード アシスタント ツール使って.NET FrameworkのWindowsフォームアプリを.NET 6に置き換える

.NET Frameworkは、バージョン4.8が最後となり、今後は、.NET 6(.NET Core)に引き継がれることになった。
いずれ、.NET Frameworkのサポートも終了となるということで、いつかは移行しなければならない。

そうは言っても.NET Frameworkと、.NETは完全互換があるわけではなく、一部機能などそのまま使えない。
小さなアプリなら.NET用に新規に作り直した方がいいと思うけど、ある程度大きなプログラムになってくると、そうも行かない。

完全ではないけど.NET Frameworkから.NET移行をしてくれる「.NET アップグレード アシスタント」なんてものがあるようなので試してみた。

参考:.NET アップグレード アシスタントの概要

 
 

もくじ

 
 

.NET Frameworkのアプリ作成

まずは、移行元サンプルとして.NET Frameworkのアプリを新規作成。
(既存アプリがある場合は、次のステップへ)
 

  1. 新規プロジェクト作成
    Visual Studioを起動し、「新しプロジェクトの作成」を選ぶ。
     

  2. .NET Frameworkを選択
    「Windowsフォームアプリケーション(.NET Framework)」を選び、「次へ」押下。

     

  3. プロジェクト作成
    「作成」押下。
    (ここではフレームワークを.NET Framework4.7.2で試した)

     

  4. ボタンを追加
    何も処理がないと寂しいので、ボタンを1つ追加。

     

    ボタンクリック時、アプリをクローズする処理を追加。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
    
  5. プロジェクトを保存

これで、移行元の.NET Frameworkアプリ準備が完了。

 
 

「.NET アップグレード アシスタント」のインストール

次に、移行に使用する「.NET アップグレード アシスタント」をインストール。
(インストール済みの場合は、次のステップへ)
コマンドプロンプトを起動し、以下のコマンドを実行。

dotnet tool install -g upgrade-assistant

 
 

.NETへの移行

これで移行準備が整ったので、以下の手順で.NET Frameworkのアプリを、.NET 6に移行する。

  1. ターミナルを表示
    再びVisual Studioに戻り、ターミナルを表示。
    表示されていない場合は、[表示]>[ターミナル]で表示。

     

  2. アップグレードの実行
    ターミナル上で以下のコマンドを実行する。

    upgrade-assistant upgrade (ソリューションまたはプロジェクトのファイル)
    

     

    今回の場合は、以下の通り。

    upgrade-assistant upgrade .\WindowsFormsApp1.sln
    


     

  3. コマンド入力
    途中、何度かコマンド選択を求められるので、その都度任意の番号を入力。

    **********************************************************************
    ** Visual Studio 2022 Developer PowerShell v17.0.5
    ** Copyright (c) 2021 Microsoft Corporation
    **********************************************************************
    PS C:\Users\osada\Desktop\WindowsFormsApp1> upgrade-assistant upgrade .\WindowsFormsApp1.sln
    -----------------------------------------------------------------------------------------------------------------
    Microsoft .NET Upgrade Assistant v0.3.261602+efb3cc30a24e699e6e4337f263a2b9478f04f618
    
    We are interested in your feedback! Please use the following link to open a survey: https://aka.ms/DotNetUASurvey
    -----------------------------------------------------------------------------------------------------------------
    
    [11:36:52 INF] Loaded 5 extensions
    [11:36:52 INF] Using MSBuild from C:\Program Files\dotnet\sdk\6.0.101\
    [11:36:52 INF] Using Visual Studio install from C:\Program Files\Microsoft Visual Studio\2022\Community [v17]
    [11:36:54 INF] Loading upgrade progress file at C:\Users\osada\Desktop\WindowsFormsApp1\.upgrade-assistant
    [11:36:54 INF] Initializing upgrade step Back up project
    
    Upgrade Steps
    
    Entrypoint: C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj
    Current Project: C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj
    
    1. [Next step] Back up project
    2. Convert project file to SDK style
    3. Clean up NuGet package references
    4. Update TFM
    5. Update NuGet Packages
    6. Add template files
    7. Update Winforms Project
        a. Default Font API Alert
        b. Winforms Source Updater
    8. Upgrade app config files
        a. Convert Application Settings
        b. Convert Connection Strings
        c. Disable unsupported configuration sections
    9. Update source code
        a. Apply fix for UA0002: Types should be upgraded
        b. Apply fix for UA0012: 'UnsafeDeserialize()' does not exist
    10. Move to next project
    
    Choose a command:
       1. Apply next step (Back up project)
       2. Skip next step (Back up project)
       3. See more step details
       4. Configure logging
       5. Exit
    > 1
    [11:36:58 INF] Applying upgrade step Back up project
    Please choose a backup path
       1. Use default path [C:\Users\osada\Desktop\WindowsFormsApp1.backup]
       2. Enter custom path
    > 1
    [11:37:04 INF] Backing up C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1 to C:\Users\osada\Desktop\WindowsFormsApp1.backup\WindowsFormsApp1
    [11:37:04 INF] Project backed up to C:\Users\osada\Desktop\WindowsFormsApp1.backup\WindowsFormsApp1
    [11:37:04 INF] Upgrade step Back up project applied successfully
    Please press enter to continue...
    
    [11:37:10 INF] Initializing upgrade step Convert project file to SDK style
    
    Upgrade Steps
    
    Entrypoint: C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj
    Current Project: C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj
    
    1. [Complete] Back up project
    2. [Next step] Convert project file to SDK style
    3. Clean up NuGet package references
    4. Update TFM
    5. Update NuGet Packages
    6. Add template files
    7. Update Winforms Project
        a. Default Font API Alert
        b. Winforms Source Updater
    8. Upgrade app config files
        a. Convert Application Settings
        b. Convert Connection Strings
        c. Disable unsupported configuration sections
    9. Update source code
        a. Apply fix for UA0002: Types should be upgraded
        b. Apply fix for UA0012: 'UnsafeDeserialize()' does not exist
    10. Move to next project
    
    Choose a command:
       1. Apply next step (Convert project file to SDK style)
       2. Skip next step (Convert project file to SDK style)
       3. See more step details
       4. Configure logging
       5. Exit
    > 1
    [11:37:19 INF] Applying upgrade step Convert project file to SDK style
    [11:37:19 INF] Converting project file format with try-convert, version 0.3.261602+8aa571efd8bac422c95c35df9c7b9567ad534ad0
    [11:37:19 INF] Recommending Windows TFM net6.0-windows because the project either has Windows-specific dependencies or builds to a WinExe
    C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj contains an App.config file. App.config is replaced by appsettings.json in .NET Core. You will need to delete App.config and migrate to appsettings.json if it's applicable to your project.
    [11:37:19 INF] Converting project C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj to SDK style
    [11:37:30 INF] Project file converted successfully! The project may require additional changes to build successfully against the new .NET target.
    [11:37:33 INF] Upgrade step Convert project file to SDK style applied successfully
    Please press enter to continue...
    
    [11:37:48 INF] Initializing upgrade step Clean up NuGet package references
    [11:37:50 INF] Reference to .NET Upgrade Assistant analyzer package (Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers, version 0.3.261602) needs to be added
    [11:37:50 INF] Packages to be added: Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers, Version=0.3.261602
    
    Upgrade Steps
    
    Entrypoint: C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj
    Current Project: C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj
    
    1. [Complete] Back up project
    2. [Complete] Convert project file to SDK style
    3. [Next step] Clean up NuGet package references
    4. Update TFM
    5. Update NuGet Packages
    6. Add template files
    7. Update Winforms Project
        a. Default Font API Alert
        b. Winforms Source Updater
    8. Upgrade app config files
        a. Convert Application Settings
        b. Convert Connection Strings
        c. Disable unsupported configuration sections
    9. Update source code
        a. Apply fix for UA0002: Types should be upgraded
        b. Apply fix for UA0012: 'UnsafeDeserialize()' does not exist
    10. Move to next project
    
    Choose a command:
       1. Apply next step (Clean up NuGet package references)
       2. Skip next step (Clean up NuGet package references)
       3. See more step details
       4. Configure logging
       5. Exit
    > 1
    [11:37:55 INF] Applying upgrade step Clean up NuGet package references
    [11:37:55 INF] Adding package reference: Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers, Version=0.3.261602
    [11:37:58 INF] Upgrade step Clean up NuGet package references applied successfully
    Please press enter to continue...
    
    [11:38:02 INF] Initializing upgrade step Update TFM
    [11:38:02 INF] Recommending Windows TFM net6.0-windows because the project either has Windows-specific dependencies or builds to a WinExe
    [11:38:02 INF] TFM needs updated to net6.0-windows
    
    Upgrade Steps
    
    Entrypoint: C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj
    Current Project: C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj
    
    1. [Complete] Back up project
    2. [Complete] Convert project file to SDK style
    3. [Complete] Clean up NuGet package references
    4. [Next step] Update TFM
    5. Update NuGet Packages
    6. Add template files
    7. Update Winforms Project
        a. Default Font API Alert
        b. Winforms Source Updater
    8. Upgrade app config files
        a. Convert Application Settings
        b. Convert Connection Strings
        c. Disable unsupported configuration sections
    9. Update source code
        a. Apply fix for UA0002: Types should be upgraded
        b. Apply fix for UA0012: 'UnsafeDeserialize()' does not exist
    10. Move to next project
    
    Choose a command:
       1. Apply next step (Update TFM)
       2. Skip next step (Update TFM)
       3. See more step details
       4. Configure logging
       5. Exit
    > 1
    [11:38:07 INF] Applying upgrade step Update TFM
    [11:38:07 INF] Recommending Windows TFM net6.0-windows because the project either has Windows-specific dependencies or builds to a WinExe
    [11:38:09 INF] Updated TFM to net6.0-windows
    [11:38:09 INF] Upgrade step Update TFM applied successfully
    Please press enter to continue...
    
    [11:38:25 INF] Initializing upgrade step Update NuGet Packages
    [11:38:27 INF] Adding Microsoft.Windows.Compatibility 6.0.0
    [11:38:27 INF] Packages to be added: Microsoft.Windows.Compatibility, Version=6.0.0
    
    Upgrade Steps
    
    Entrypoint: C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj
    Current Project: C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj
    
    1. [Complete] Back up project
    2. [Complete] Convert project file to SDK style
    3. [Complete] Clean up NuGet package references
    4. [Complete] Update TFM
    5. [Next step] Update NuGet Packages
    6. Add template files
    7. Update Winforms Project
        a. Default Font API Alert
        b. Winforms Source Updater
    8. Upgrade app config files
        a. Convert Application Settings
        b. Convert Connection Strings
        c. Disable unsupported configuration sections
    9. Update source code
        a. Apply fix for UA0002: Types should be upgraded
        b. Apply fix for UA0012: 'UnsafeDeserialize()' does not exist
    10. Move to next project
    
    Choose a command:
       1. Apply next step (Update NuGet Packages)
       2. Skip next step (Update NuGet Packages)
       3. See more step details
       4. Configure logging
       5. Exit
    > 1
    [11:38:39 INF] Applying upgrade step Update NuGet Packages
    [11:38:39 INF] Adding package reference: Microsoft.Windows.Compatibility, Version=6.0.0
    [11:38:48 INF] Upgrade step Update NuGet Packages applied successfully
    Please press enter to continue...
    
    [11:39:02 INF] Initializing upgrade step Add template files
    [11:39:02 INF] 0 expected template items needed
    [11:39:02 INF] Initializing upgrade step Update Winforms Project
    [11:39:02 WRN] Default font in Windows Forms has been changed from Microsoft Sans Serif to Seg Segoe UI, in order to change the default font use the API - Application.SetDefaultFont(Font font). For more details see here - https://devblogs.microsoft.com/dotnet/whats-new-in-windows-forms-in-net-6-0-preview-5/#application-wide-default-font.
    [11:39:02 WRN] HighDpiMode needs to set in Main() instead of app.config or app.manifest - Application.SetHighDpiMode(HighDpiMode.<setting>). It is recommended to use SystemAware as the HighDpiMode option for better results.
    
    Upgrade Steps
    
    Entrypoint: C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj
    Current Project: C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj
    
    1. [Complete] Back up project
    2. [Complete] Convert project file to SDK style
    3. [Complete] Clean up NuGet package references
    4. [Complete] Update TFM
    5. [Complete] Update NuGet Packages
    6. [Complete] Add template files
    7. Update Winforms Project
        a. [Next step] Default Font API Alert
        b. Winforms Source Updater
    8. Upgrade app config files
        a. Convert Application Settings
        b. Convert Connection Strings
        c. Disable unsupported configuration sections
    9. Update source code
        a. Apply fix for UA0002: Types should be upgraded
        b. Apply fix for UA0012: 'UnsafeDeserialize()' does not exist
    10. Move to next project
    
    Choose a command:
       1. Apply next step (Default Font API Alert)
       2. Skip next step (Default Font API Alert)
       3. See more step details
       4. Configure logging
       5. Exit
    > 1
    [11:39:06 INF] Applying upgrade step Default Font API Alert
    [11:39:06 WRN] Default font in Windows Forms has been changed from Microsoft Sans Serif to Seg Segoe UI, in order to change the default font use the API - Application.SetDefaultFont(Font font). For more details see here - https://devblogs.microsoft.com/dotnet/whats-new-in-windows-forms-in-net-6-0-preview-5/#application-wide-default-font.
    [11:39:06 INF] Upgrade step Default Font API Alert applied successfully
    Please press enter to continue...
    
    
    Upgrade Steps
    
    Entrypoint: C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj
    Current Project: C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj
    
    1. [Complete] Back up project
    2. [Complete] Convert project file to SDK style
    3. [Complete] Clean up NuGet package references
    4. [Complete] Update TFM
    5. [Complete] Update NuGet Packages
    6. [Complete] Add template files
    7. Update Winforms Project
        a. [Complete] Default Font API Alert
        b. [Next step] Winforms Source Updater
    8. Upgrade app config files
        a. Convert Application Settings
        b. Convert Connection Strings
        c. Disable unsupported configuration sections
    9. Update source code
        a. Apply fix for UA0002: Types should be upgraded
        b. Apply fix for UA0012: 'UnsafeDeserialize()' does not exist
    10. Move to next project
    
    Choose a command:
       1. Apply next step (Winforms Source Updater)
       2. Skip next step (Winforms Source Updater)
       3. See more step details
       4. Configure logging
       5. Exit
    > 1
    [11:39:20 INF] Applying upgrade step Winforms Source Updater
    [11:39:20 WRN] HighDpiMode needs to set in Main() instead of app.config or app.manifest - Application.SetHighDpiMode(HighDpiMode.<setting>). It is recommended to use SystemAware as the HighDpiMode option for better results.
    [11:39:20 INF] Updated Program.cs file at C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\Program.cs with HighDPISetting set to SystemAware
    [11:39:20 INF] Upgrade step Winforms Source Updater applied successfully
    [11:39:20 INF] Applying upgrade step Update Winforms Project
    [11:39:20 INF] Upgrade step Update Winforms Project applied successfully
    Please press enter to continue...
    
    [11:39:24 INF] Initializing upgrade step Upgrade app config files
    [11:39:24 INF] Found 0 app settings for upgrade:
    [11:39:24 INF] Found 0 connection strings for upgrade:
    [11:39:24 INF] Initializing upgrade step Update source code
    [11:39:24 INF] Running analyzers on WindowsFormsApp1
    [11:39:25 INF] Identified 0 diagnostics in project WindowsFormsApp1
    [11:39:25 INF] Initializing upgrade step Move to next project
    
    Upgrade Steps
    
    Entrypoint: C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj
    Current Project: C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj
    
    1. [Complete] Back up project
    2. [Complete] Convert project file to SDK style
    3. [Complete] Clean up NuGet package references
    4. [Complete] Update TFM
    5. [Complete] Update NuGet Packages
    6. [Complete] Add template files
    7. [Complete] Update Winforms Project
        a. [Complete] Default Font API Alert
        b. [Complete] Winforms Source Updater
    8. [Complete] Upgrade app config files
        a. [Complete] Convert Application Settings
        b. [Complete] Convert Connection Strings
        c. [Complete] Disable unsupported configuration sections
    9. [Complete] Update source code
        a. [Complete] Apply fix for UA0002: Types should be upgraded
        b. [Complete] Apply fix for UA0012: 'UnsafeDeserialize()' does not exist
    10. [Next step] Move to next project
    
    Choose a command:
       1. Apply next step (Move to next project)
       2. Skip next step (Move to next project)
       3. See more step details
       4. Configure logging
       5. Exit
    > 1
    [11:39:31 INF] Applying upgrade step Move to next project
    [11:39:31 INF] Upgrade step Move to next project applied successfully
    Please press enter to continue...
    
    [11:39:33 INF] Recommending Windows TFM net6.0-windows because the project either has Windows-specific dependencies or builds to a WinExe
    [11:39:33 INF] Recommending Windows TFM net6.0-windows because the project either has Windows-specific dependencies or builds to a WinExe
    [11:39:33 INF] Recommending Windows TFM net6.0-windows because the project either has Windows-specific dependencies or builds to a WinExe
    [11:39:33 INF] Initializing upgrade step Finalize upgrade
    
    Upgrade Steps
    
    Entrypoint: C:\Users\osada\Desktop\WindowsFormsApp1\WindowsFormsApp1\WindowsFormsApp1.csproj
    
    [11:39:33 INF] Recommending Windows TFM net6.0-windows because the project either has Windows-specific dependencies or builds to a WinExe
    1. [Next step] Finalize upgrade
    
    Choose a command:
       1. Apply next step (Finalize upgrade)
       2. Skip next step (Finalize upgrade)
       3. See more step details
       4. Configure logging
       5. Exit
    > 1
    [11:39:36 INF] Applying upgrade step Finalize upgrade
    [11:39:36 INF] Upgrade step Finalize upgrade applied successfully
    Please press enter to continue...
    
    [11:39:38 INF] Upgrade has completed. Please review any changes.
    [11:39:38 INF] Deleting upgrade progress file at C:\Users\osada\Desktop\WindowsFormsApp1\.upgrade-assistant
    

 

これで移行完了。
 
 

ちなみに、途中(2. Convert project file to SDK style)で、以下のような警告メッセージボックスが表示された。

ファイル変更の検出
プロジェクト 'WindowsFormsApp1' は環境外で変更されています。
変更されたプロジェクトをディスクから読み込むには[再読み込み]を押します。
外部の変更と操作がすべて完了したら[すべて再読み込み]を押して、ディスクからすべての項目を読み込みます。
外部の変更を無視する場合は、[無視]を押します。この変更は、次回プロジェクトを開いたときに使用されます。
    [再読み込み] [すべて再読み込み] [無視] [すべて無視]

&nbps;
なんだろう。。
作業中に外部から変更は加えていないし、ディスクも使っていない。
きっとアップグレード中に、別プロセスがファイル更新したとかなんだろうか。
何度やり直しても同じ。
今回は「すべて再読み込み」ボタンを押下したが、特に問題なかった。
 

.NET 6 で実行

改めて変換されたプロジェクトのプロパティを確認してみると、ターゲットフレームワークが「.NET 6.0」となっている。

 

.NET6での実行も大丈夫そう。

 
 

今回は、ボタン1個しかない単純なWindowsフォームアプリだったから、特に問題なかったけど、ゴリゴリ作り込んだアプリはそんな単純には移行できないかも知れない。

 
 

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA


*