Register DLL in GAC (CMD or PowerShell)

与世无争的帅哥 提交于 2021-02-18 16:59:30

问题


I'm trying to register a .DLL in the GAC. Currently I'm having trouble to prove it has been added to the assembly.

Using the command

C:\Windows\System32>%programfiles(x86)%\Microsoft SDKs\Windows\v7.0A\Bin\gacutil.exe -i "path\to\my\file.dll"

The prompt tells me that the assembly has been added to the cache.

When checking with

C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin>gacutil.exe -l file.dll

It says that there are 0 elements in the assembly.

Via Powershell commands, I'm trying to add the DLL like this:

PS C:\WINDOWS\system32> Add-Type -AssemblyName "System.EnterpriseServices"

PS C:\WINDOWS\system32> $publish = New-Object System.EnterpriseServices.Internal.Publish

PS C:\WINDOWS\system32> $publish.GacInstall("file.dll")

What did I do wrong? How can I add the .DLL to the GAC? The best way (for me) would be doing that with Powershell.


回答1:


Remember to run PowerShell as administrator or this won't work.

$dllpath = c:\path\yourdll.dll
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")            
$publish = New-Object System.EnterpriseServices.Internal.Publish            
$publish.GacInstall($dllpath)

You will likely need to restart whatever related service or program after this, for example:

if (Get-Service "YourService" -ErrorAction SilentlyContinue)
    {
        Stop-Service -Name 'YourService' 
        Start-Service -Name 'YourService' 

    }

Or just restart your computer.




回答2:


I managed to solve it with these Powershell lines:

Set-location "C:\Temp"
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall("C:\Temp\file.dll")

After restarting the PC/Server, the dll is registered in the GAC. Thanks for the help! :)




回答3:


Use the following *.bat to install or update DLL to GAC using GacUtil.exe within the visual studio developer command prompt

@ECHO OFF
cls
ECHO ------: GAC UPDATER :-----
ECHO -                        -
ECHO  - Updating  %1
gacutil /if %1

Save this as install.bat and call it with a parameter like install abcd.dll the %1 will receive the parameter as text and will serve Gacutil as gacutil /if abcd.dll, the /if here forces to install the dll thus updating it in case it already available.

In absence of the GacUtil.exe you can use the powershell script below

Set-location (Get-Location).Path
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall((Get-Location).Path + "\YourFile.dll")

This powershell script gets the current location of itself and assumes the YourFile.dll to be next to it, in case the dll is in some remote location update the script to replace $publish.GacInstall((Get-Location).Path + "\YourFile.dll") with $publish.GacInstall("path to \YourFile.dll")

Save the script as install.ps1remember to replace the YourFile.dll with your dll name and place it next to the the script file then execute the script




回答4:


Check the filename without the extension (.dll) C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin>gacutil.exe -l file



来源:https://stackoverflow.com/questions/45593418/register-dll-in-gac-cmd-or-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!