inzo21 Posted January 8, 2004 Posted January 8, 2004 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 Quote he who forgets will be destined to remember... (E.Vedder)
*Gurus* divil Posted January 8, 2004 *Gurus* Posted January 8, 2004 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. It's in System.Reflection. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
inzo21 Posted January 8, 2004 Author Posted January 8, 2004 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 Quote he who forgets will be destined to remember... (E.Vedder)
*Gurus* divil Posted January 8, 2004 *Gurus* Posted January 8, 2004 Yes, you use string manipulation to get the directory name from the full path. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
inzo21 Posted January 9, 2004 Author Posted January 9, 2004 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 Quote he who forgets will be destined to remember... (E.Vedder)
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.