Click here to Skip to main content
15,916,463 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I created a class library project and added X.dll and y.dll and I am building it. It creates Classlibrary.dll. This classlibrary.dll consists of X.dll and y.dll.

Now I am creating a new Winforms application and adding ClassLibrary.dll as a reference. Now if I run the Winforms application I want to find the reference path of X.dll and Y.dll

I tried this but it shows Winforms local bin folder as the path

C#
MyAssembly = Assembly.LoadFrom("Classlibrary.Dll");
  string asembl = GetCurrentExecutingDirectory(MyAssembly);


 public static string GetCurrentExecutingDirectory(Assembly assemb)

{

       string filePath = new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath;

         return Path.GetDirectoryName(filePath);
     }

It shows only local path folder like D:\Winform\bin\debug\Winform.dll or winform.exe ... this is not right . i need path from GAC ..pls any one help me
Posted
Comments
Sergey Alexandrovich Kryukov 10-Mar-12 21:14pm    
What does it mean: "This classlibrary.dll consists of X.dll and y.dll". How it consists?
--SA
Sergey Alexandrovich Kryukov 10-Mar-12 21:16pm    
Is "ClassLibrary.DLL" a .NET Assembly or not?
Is there any reason to load DLL instead of referencing it?
--SA
Sergey Alexandrovich Kryukov 10-Mar-12 21:34pm    
Why path from GAC? It's never really needed.
--SA

1 solution

Roughly speaking, the whole purpose of GAC is that you never need a path from it. You just reference an assembly by its full name. To see how it's done in the project, please see how some assembly bundled with .NET is referenced, such as "System.Windows.Forms.dll". Please also see my comments to the question.

Even if you need to load the assembly from the GAC dynamically, you should rather use System.Reflection.Assembly.Load(string assemblyString), please see the example on this page:
http://msdn.microsoft.com/en-us/library/ky3942xh.aspx[^].

You code for "current executing directory" is questionable. You can have 1) location of the assembly where you function is called; 2) more usually used, the location of the main module of the entry assembly; 3) working directory.
Note that different assemblies of your application can be placed in different directories; there a different methods of assembly resolution, based on config file or code.

Right methods would be:
C#
static string GetAssemblyDirectory(System.Reflection.Assembly assembly) {
    return System.IO.Path.GetDirectoryName(assembly.Location);
}

//...

string callerAssemblyLocation = GetAssemblyDirectory(System.Reflection.Assembly.GetExecutingAssembly());
string entryAssemblyLocation = GetAssemblyDirectory(System.Reflection.Assembly.GetEntryAssembly());
string workingDirectory = System.IO.Directory.GetCurrentDirectory();


Please see:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx[^].

—SA
 
Share this answer
 
v3
Comments
ProEnggSoft 11-Mar-12 1:09am    
Is CAG another acronym? Just got doubt as it is mentioned at 3 places.
Sergey Alexandrovich Kryukov 12-Mar-12 4:33am    
Sorry, I misspelled it. This is GAC, Global Assembly Cache. It contains globally-visible assemblies named by their strong name, not by file name. All assemblies bundled with .NET are there, and you can add yours.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900