LoginSignup
8
6

More than 5 years have passed since last update.

パスが通っていない regasm.exe を呼出する

Posted at

はじめに

.NET Framwork マネージドコードの COM オブジェクトをシステムに登録するには、regasm.exe を使います。
ところがこれには標準でパスが通っていません。
フルパスで指定するにしても実行する環境によってパスが異なります。
(自分の開発環境では C:\Windows\Microsoft.NET\Framework\v4.0.30319 だが、別の環境ではバージョン番号の箇所が異なる。)
自作の COM オブジェクトを配付してインストーラで自動で登録させるのに、これでは困ります。

regasm.exe は .NET Framework のインストール先です。
.NET のコードで実行時に Framework のインストール先が取得できるので、regasm.exe を呼出するプログラムを作ればいいのでは、と思いました。

regasm.exe を呼出するプログラム

プロジェクトを新規作成します。「コンソールアプリケーション」を選びます。

Main プロシージャを実装します。

Program.cs
using System.Runtime.InteropServices;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        // RegAsm のパスを取得
        string path = System.IO.Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "RegAsm.exe");
        // パスをコンソールに出力
        Console.WriteLine("[" + path + "]");

        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo.FileName = path;
        // 渡されたコマンドライン引数をそのまま渡す
        StringBuilder buff = new StringBuilder(128);
        foreach(string arg in args)
        {
            buff.Append(arg + " ");
        }
        p.StartInfo.Arguments = buff.ToString();
        // 出力を取得できるようにする
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardInput = false;
        // ウィンドウを表示しない
        p.StartInfo.CreateNoWindow = true;

        // 起動
        p.Start();

        // 出力を取得
        string results = p.StandardOutput.ReadToEnd();
        // プロセス終了まで待機する
        p.WaitForExit();
        p.Close();
        // 出力された結果を表示
        Console.WriteLine(results);
    }
}
Program.vb
Imports System.Runtime.InteropServices
Imports System.Text

Module Program

    Sub Main()

        'RegAsm のパスを取得
        Dim path As String = IO.Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "RegAsm.exe")
        'パスをコンソールに出力
        Console.WriteLine("[" & path & "]")

        Dim p As New System.Diagnostics.Process()
        p.StartInfo.FileName = path
        '渡されたコマンドライン引数をそのまま渡す
        Dim args As New StringBuilder(128)
        For Each arg As String In My.Application.CommandLineArgs
            args.Append(arg & Space(1))
        Next
        p.StartInfo.Arguments = args.ToString
        '出力を取得できるようにする
        p.StartInfo.UseShellExecute = False
        p.StartInfo.RedirectStandardOutput = True
        p.StartInfo.RedirectStandardInput = False
        'ウィンドウを表示しない
        p.StartInfo.CreateNoWindow = True

        '起動
        p.Start()

        '出力を取得
        Dim results As String = p.StandardOutput.ReadToEnd()
        'プロセス終了まで待機する
        p.WaitForExit()
        p.Close()
        '出力された結果を表示
        Console.WriteLine(results)
    End Sub

End Module
8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6