简体   繁体   English

如何在 C# 中以编程方式注册 COM 程序集?

[英]How do you register COM assemblies programmatically in C#?

I guess technically, it doesn't have to be a COM assembly specifically.我想从技术上讲,它不一定是 COM 组件。 But, I wanted to include that because that's how I searched for it myself.但是,我想包含它,因为这就是我自己搜索它的方式。

Currently, we use a batch file that registers them with the following line:目前,我们使用一个批处理文件,通过以下行注册它们:

c:\windows\microsoft.net\framework\v4.0.30319\regasm.exe c:\Path\To\AssemblyToRegister.dll /codebase /s /tlb

How would you register it in C# code so we can get rid of the batch files and automate this process a little better?您将如何在 C# 代码中注册它,以便我们可以摆脱批处理文件并更好地自动化此过程?

Note: The following code requires the application to be run as administrator!注意:以下代码需要以管理员身份运行应用程序!

Assembly asm = Assembly.LoadFrom(@"c:\Path\To\AssemblyToRegister.dll");
RegistrationServices regAsm = new RegistrationServices();
bool bResult = regAsm.RegisterAssembly(asm, AssemblyRegistrationFlags.SetCodeBase);

I found this answer originally from this page .我最初从这个页面找到了这个答案。 However, we ran into an issue where it threw an exception on the third line with a certain dll and found that the "LoadFile" form line 1 should have been "LoadFrom".但是,我们遇到了一个问题,它在第三行抛出了一个带有某个 dll 的异常,并发现表单第 1 行的“LoadFile”应该是“LoadFrom”。 Here is the link to the Stack Overflow question where we found the fix. 是我们找到修复程序的 Stack Overflow 问题的链接。

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.此代码假定dllLocation变量中提供了 dll 的绝对路径。 You might also have to change the regasm location value to wherever it's installed on your machine.您可能还必须将 regasm 位置值更改为它安装在计算机上的任何位置。

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://docs.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.这是有限的,因为它通过控制台 window 运行 regasm。 This means that you can't really handle exceptions.这意味着您无法真正处理异常。 I would suggest doing it programmatically with my other answer.我建议用我的其他答案以编程方式进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM