Here is my first attempt which is not the best way of doing this. I am adding in-case it helps someone in the future.
This code assumes an absolute path to the dll is provided in the dllLocation variable.
You might also have to change the regasm location value to wherever it's installed on your machine.
string regasmLocation = @"c:\windows\microsoft.net\framework\v4.0.30319\regasm.exe";
try
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = regasmLocation;
// If you want the tlb (type library) created use the following.
//p.StartInfo.Arguments = dllLocation + @" /codebase /s /tlb";
p.StartInfo.Arguments = dllLocation + @" /codebase /s";
p.Start();
// To avoid deadlocks, always read the output stream first and then wait. https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standarderror?view=net-6.0
string standardError = p.StandardError.ReadToEnd();
string standardOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine($"Successfully registered '{dllLocation}'.");
if (!string.IsNullOrWhiteSpace(standardOutput))
{
Console.WriteLine($"Standard Output: \n{standardOutput}");
}
if (!string.IsNullOrWhiteSpace(standardError))
{
Console.WriteLine($"Standard Error: \n{standardError}");
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
return false;
}
return true;
This is limited because it runs regasm through a console window. This means that you can't really handle exceptions. I would suggest doing it programmatically with my other answer.