how to get path of .dll or assembly

inzo21

Well-known member
Joined
Nov 5, 2003
Messages
74
Hi everyone,

Im trying to set a string value to equal the path of the executing .dll called when I instantiate an object from that class. This class is used to read and write values to a config file for an application.

I instantiate it from various access points throughout the application to get and set these properties. In the constructor however, I check to see if the config file exists and, if it doesnt, I create a new one with default values. Currently I am using the follwing syntex for this:

Private FileLocation As String = Environment.CurrentDirectory & "\configfile.xml"

Sub New()
Dim thefile As FileInfo = New FileInfo(FileLocation)
If Not (thefile.Exists) Then LoadDefaultValues()
Try
configfile.Load(FileLocation)
Catch e As Exception
Console.WriteLine("Exception: {0}", e.ToString())
End Try
LoadAllValues()
FileLocation = m_appdirectory
End Sub

The problem I have is when I instantiate a new object of this class from a directory other than where the .dll is, it always creates the configfile.xml file in that directory. I cant hardcode it into the FileLocation path because the user will determine the installation directory.

Thus, is there a way, using reflection or some other technique, to set the file location to always be the current directory of the .dll that is being called?

thanks in advance.

inzo
 
You can use the various properties of the Assembly class to find out the codebase of the dll that started your process, or the dll the currect method is running in. Its in System.Reflection.
 
tried that

Thanks Divil,

I did try to use both the codebase and the location property of the system.reflection.assembly class.

This, however, is giving me the full url to the actuall .dll. I need the directory that the .dll resides in. I know I could parse out the name of the .dll using text formatting but wanted to see if there existed a property or easier way to get this info without going through that.

thanks again for your prompt response though and please, let me know if you think of anything.

inzo
 
Yes, you use string manipulation to get the directory name from the full path.
 
solution

Divil,

Thanks again for helping out. Im posting how I got the information for anybody that was following the thread that needs this info.

I placed this info in the class declaration of my .dll rather than in the constructor

Dim myassembly As System.Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly()
Dim tempfile As FileInfo = New FileInfo(myassembly.Location)
Private FileLocation As String = tempfile.DirectoryName & "\config.xml"

this did the trick and always refers to the directory where the executing .DLL resides in.

Thanks again,

inzo
 
Back
Top