Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi everyone,

 

I'm 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 doesn't, 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 can't 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

he who forgets will be destined to remember... (E.Vedder)
Posted

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

he who forgets will be destined to remember... (E.Vedder)
Posted

solution

 

Divil,

 

Thanks again for helping out. I'm 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

he who forgets will be destined to remember... (E.Vedder)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...