Configuration for DLL

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi Guys and Girls,

I have a situation where I am wishing to deploy a .NET dll onto a number of
servers. The classes in the DLL will be used by VBScripts. When one of the
classes - Connection - is instantiated from a VBScript the code looks to a
simple text file for initialisation of certain variables. I want this text
file to reside with the dll when it is installed (via a setup project) and
have the file opened by the Connection class in the installation directory.
At the moment I have:

StreamReader sr = new StreamReader(@"config.ini");

and I use this stream to read in values. However, the path I have specified
here will be looking, when the object is made in the VBscript, where the
VBScript is located. How can I get it to look at the installation directory?

Any suggestions would be good,
Thanks,
Jesse
 
Jessard said:
Hi Guys and Girls,

I have a situation where I am wishing to deploy a .NET dll onto a number of
servers. The classes in the DLL will be used by VBScripts. When one of the
classes - Connection - is instantiated from a VBScript the code looks to a
simple text file for initialisation of certain variables. I want this text
file to reside with the dll when it is installed (via a setup project) and
have the file opened by the Connection class in the installation directory.
At the moment I have:

StreamReader sr = new StreamReader(@"config.ini");

and I use this stream to read in values. However, the path I have specified
here will be looking, when the object is made in the VBscript, where the
VBScript is located. How can I get it to look at the installation directory?

Any suggestions would be good,
Thanks,
Jesse

If I understand you scenario correctly you could simply use
..NET's built-in ConfigurationSettings functionality. If your
assembly is called "MyConnection.dll" create a configuration
file "MyConnection.dll.config" that resides in the same
directory as the assembly. Add your connection string to it:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="MyDatabase.Connection"

value="server=(local);database=mydatabase;Trusted_Connection=true"
/>
</appSettings>
</configuration>

So here the "key" (whatever you name it) merely gives you a
handle to the "value" which happens to contain your
connection string.

In your class simply use:

using System.Configuration;
...

string connectionString =

ConfigurationSettings.AppSettings.Get("MyDatabase.Connection");

or even

private SqlConnection connection =
new SqlConnection(

ConfigurationSettings.AppSettings.Get("MyDatabase.Connection")
);
connection.Open();

or whatever the equivalent VB.NET is.


"HOW TO: Store and Retrieve Custom Information from an
Application Configuration File by Using Visual C# .NET"
http://support.microsoft.com/default.aspx?scid=kb;en-us;815786


"How To Store and Retrieve Custom Information from an
Application Configuration File by Using Visual Basic .NET"
http://support.microsoft.com/kb/313405/EN-US/


If you have a username and password that could use some
obscuring, then investigate the aspnet_setreg.exe utility
that was originally developed for ASP.NET.

"How to use the ASP.NET utility to encrypt credentials and
session state connection strings"
http://support.microsoft.com/default.aspx?scid=kb;en-us;329290


If you still need to know the where that assembly is then
this MAY be of some help:

"HOW TO: Determine the Executing Application's Path"
http://msdn.microsoft.com/library/d...n-us/dncfhowto/html/howtoexecutingapppath.asp


'Any fool can write code that a computer can understand.
Good programmers write code that humans can understand.'
Martin Fowler,
'Refactoring: improving the design of existing code', p.15
 
perhaps you can use this call:
System.Reflection.Assembly.GetExecutingAssembly().Location

--- Nick

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
UAError said:
If I understand you scenario correctly you could simply use
.NET's built-in ConfigurationSettings functionality. If your
assembly is called "MyConnection.dll" create a configuration
file "MyConnection.dll.config" that resides in the same
directory as the assembly. Add your connection string to it:

Nope. DLLs do not have config files. Take a look at the links you give. The
clue is in the title "from an *Application* Configuration File"
"HOW TO: Store and Retrieve Custom Information from an
Application Configuration File by Using Visual C# .NET"
http://support.microsoft.com/default.aspx?scid=kb;en-us;815786

ConfigSettings.GetConfig will retrieve values from the *application's*
configuration file.

Take a look at this:

http://www.grimes.demon.co.uk/dotnet/configFAQ.htm#003

Also, since the OP said he will use VBScript the config file will be for the
process that runs the VBScript file (cscript.exe) in which case the config
file would have to be in the same folder as that file (and be called
cscript.exe.config).

Richard
 
Richard Grimes said:
Nope. DLLs do not have config files. Take a look at the links you give. The
clue is in the title "from an *Application* Configuration File"


ConfigSettings.GetConfig will retrieve values from the *application's*
configuration file.

Take a look at this:

http://www.grimes.demon.co.uk/dotnet/configFAQ.htm#003


Silly me, I should have know better. The reason I wrote this
was because that's the way NUnit (
www.nunit.com
http://www.microsoft.com/MSPress/books/6778.asp
) works - but of course I now realize that it uses
"CreateInstanceAndUnwrap" to make it behave that way (as you
outline in
http://www.grimes.demon.co.uk/dotnet/configFAQ.htm#001
)

So I guess the OP is limited to something like

"A custom configuration file AppSettings reader class"
http://www.codeproject.com/csharp/Custom_Config_File_Reader.asp

or rolling his own ala:

"Application Configuration Using Configuration Files"
on "Visual C# .NET Code Samples"
http://www.microsoft.com/downloads/...eb-7152-454d-9936-495ffd79afd0&displaylang=en

I suspect that the "AppSettings" class in the

"Framework - How-To Configuration Settings" project in
"101 Visual Basic .NET Code Samples"
http://www.microsoft.com/downloads/...f8-033d-420b-a3b1-3074505c03f3&DisplayLang=en

does something similar.

So the OP will still have to use

"System.Reflection.Assembly.GetExecutingAssembly().Location"
http://msdn.microsoft.com/library/d...ionassemblyclassgetexecutingassemblytopic.asp
http://msdn.microsoft.com/library/d...ystemreflectionassemblyclasslocationtopic.asp

FileInfo AssemblyLocation = new FileInfo (

System.Reflection.Assembly.GetExecutingAssembly().Location
);
string assemblyDirectory =
AssemblyLocation.Directory.FullName;

to get the assembly's directory.
Also, since the OP said he will use VBScript the config file will be for the
process that runs the VBScript file (cscript.exe) in which case the config
file would have to be in the same folder as that file (and be called
cscript.exe.config).

Richard


'Any fool can write code that a computer can understand.
Good programmers write code that humans can understand.'
Martin Fowler,
'Refactoring: improving the design of existing code', p.15
 
Back
Top