Search code examples
c#.netdllregasm

c# register dll using the file path (RegistrationService.RegisterAssembly)


I want create a simple console application to automatically register a dll.

After some research I found this piece of code:

public static bool Register_Dlls( string dllPath )
{
  Assembly asm = Assembly.LoadFile( dllPath );
  RegistrationServices regAsm = new RegistrationServices();
  bool bResult = regAsm.RegisterAssembly( asm, AssemblyRegistrationFlags.SetCodeBase );
  return bResult;
}

Implementing my application using that code and running it as administrator the dll can not be registered. This is my problem.

But using the RegAsm command inside the cmd (as administrator) then the dll is registered correctly:

  C:\windows\system32> C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe C:\PWS\Bin\MyContextMenu.dll /codebase

My question is: am I forgetting something or the above code can not be used for my purpose?

The dll has been compiled in 'ANY CPU' Solution Platforms mode, like my application, and I need to register the assembly on a x64 machine.


Solution

  • Solution:

    I compiled my application forcing 'x64' Solution Platforms mode, now everything works fine.

    Moreover, following the last Hans Passant comment, I improved my code modifying the manifest in its requestedExecutionLevel field and adding the IsUserAdministrator() method. Thanks Hans!