アプリケーションを配布する(.NET)

.NET アプリケーションは、デバッグ ビルドとリリース ビルドの 2 つの展開可能なビルドで配布できます。

アプリケーションを配布するビルドの種類を選択する必要があります。どちらの種類のビルドも AutoCAD にロードすることができます。通常、デバッグ ビルドはアプリケーションの開発およびテスト時にのみ使用し、リリース ビルドは社内外の多数のコンピュータで使用するアプリケーションの配布時に構築します。

.NET アセンブリのリリース ビルドを生成するには

次の手順では、.NET アセンブリのリリース ビルドを生成する方法について説明します。

  1. Microsoft Visual Studio で、リリース ビルドを生成するプロジェクトを開きます。
  2. [ビルド]メニュー[構成マネージャ]の順にクリックします。
  3. [構成マネージャ]の[アクティブ ソリューション構成]ドロップダウン リストで[リリース]を選択します。
  4. [閉じる]をクリックします。
  5. Microsoft Visual Studio で、[ビルド]メニュー [ソリューションのビルド]の順にクリックします。

.NET アセンブリをロードする

.NET アセンブリのビルドの種類を決定したら、AutoCAD にロードする方法を決定する必要があります。.NET アセンブリ ファイルは、手動でロードまたはディマンド ロードすることができます。

.NET アプリケーションをディマンド ロードする

次の例では、AutoCAD の起動時に .NET アセンブリ ファイルをロードするために、必要なキーをレジストリに作成して削除します。RegisterMyApp コマンドを使用すると、AutoCAD の次回起動時にアプリケーションを自動的にロードするために必要なレジストリ キーが作成されます。UnregisterMyApp コマンドを使用すると、AutoCAD の次回起動時にアプリケーションをロードしないように、ディマンド ロード情報がレジストリから削除されます。

C# の例

using Microsoft.Win32;
using System.Reflection;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;

[CommandMethod("RegisterMyApp")]
public void RegisterMyApp()
{
  // Get the AutoCAD Applications key
  string sProdKey = HostApplicationServices.Current.RegistryProductRootKey;
  string sAppName = "MyApp";

  RegistryKey regAcadProdKey = Registry.CurrentUser.OpenSubKey(sProdKey);
  RegistryKey regAcadAppKey = regAcadProdKey.OpenSubKey("Applications", true);

  // Check to see if the "MyApp" key exists
  string[] subKeys = regAcadAppKey.GetSubKeyNames();
  foreach (string subKey in subKeys)
  {
      // If the application is already registered, exit
      if (subKey.Equals(sAppName))
      {
          regAcadAppKey.Close();
          return;
      }
  }

  // Get the location of this module
  string sAssemblyPath = Assembly.GetExecutingAssembly().Location;

  // Register the application
  RegistryKey regAppAddInKey = regAcadAppKey.CreateSubKey(sAppName);
  regAppAddInKey.SetValue("DESCRIPTION", sAppName, RegistryValueKind.String);
  regAppAddInKey.SetValue("LOADCTRLS", 14, RegistryValueKind.DWord);
  regAppAddInKey.SetValue("LOADER", sAssemblyPath, RegistryValueKind.String);
  regAppAddInKey.SetValue("MANAGED", 1, RegistryValueKind.DWord);

  regAcadAppKey.Close();
}

[CommandMethod("UnregisterMyApp")]
public void UnregisterMyApp()
{
  // Get the AutoCAD Applications key
  string sProdKey = HostApplicationServices.Current.RegistryProductRootKey;
  string sAppName = "MyApp";

  RegistryKey regAcadProdKey = Registry.CurrentUser.OpenSubKey(sProdKey);
  RegistryKey regAcadAppKey = regAcadProdKey.OpenSubKey("Applications", true);

  // Delete the key for the application
  regAcadAppKey.DeleteSubKeyTree(sAppName);
  regAcadAppKey.Close();
}