Install an assembly into GAC programmatically

倾然丶 夕夏残阳落幕 提交于 2019-12-04 12:19:47

问题


I need to install an assembly in GAC using c#. Below is my code:

new System.EnterpriseServices.Internal.Publish().GacInstall("MyAssembly.dll");

The above code gives the error:

Absolute path required

But i need this to run without using static file path (absolute path). Can anyone tell me whether its possible? I have added the reference to the assembly inside the project references. I need to install this assembly inside GAC.


回答1:


You could do something like:

GacInstall((new System.IO.FileInfo("MyAssembly.dll")).FullName);

or,

GacInstall(System.IO.Path.GetFullPath("MyAssembly.dll"));

Assuming that the file is in your current working directory. If it's not, then you need to define what rules are being used to find this DLL (e.g. is it in the same path as your current executable?)




回答2:


try this below piece I cameup with, let me know if this works

Assembly assembly = Assembly.GetAssembly(typeof(Program));//Replace your Type here.
string filePath = assembly.Location; 

Then use this file path.




回答3:


If you know relative path of that DLL in regard of your executeble, make

string executableDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string assemblyFullPath = Path.Combine(executableDirectory,
                            relativePathToAssembly);

Should work for you.




回答4:


Error itself states, that you need to give full location path of the dll residing. ie C:\myprojects\myassembly.dll in the path




回答5:


Simple steps to add a ".dll" file into the Global Assembly Cache in c#:

  1. In your project, "Add reference" to the .dll file you want to install in the GAC
  2. Form the References, right click on the particular .dll file and select "Properties"
  3. Change the property "Copy Local" to "True"
  4. Build and deploy using wspBuilder
  5. For verification, just check in the manifest file whether the path exists or not and also in "C:\Windows\assembly".


来源:https://stackoverflow.com/questions/12382831/install-an-assembly-into-gac-programmatically

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