Using .Config Files | | Not too long ago, I was working on a standard app that we were going to deploy at a few customer sites. One of the requirements of the project though is that I couldn't touch the registry. Security settings were such that I couldn't write to it so had to use another means (it doesn't seem so long ago that there wasn't even a registry).
Anyway, .NET gives you a great little mechanism for storing configuration information like connection strings, configuration files. There are a hundred zillion reasons not to hard code connection strings or anything else in your program, but that's another issue. So, if you are using ASP.NET you'll have a web.config file already, if not, add a Configuration file Project, Add New Iterm, Configuration File. Now add something like this to it:
<appSettings>
<add key="ConnectString" value="UID=xxx; PWD=xxxxx; Data Source=xxx;Initial Catalog=xxx"/>
<add key="DefaultDB" value = "SomeDataBase"/>
<add key="Cipher" value = "SomeCipher"/>
appSettings> |
I use the Key ConnectionString for my connect string (gasp), reference a DefaultDB and a Cipher which is used for encrypting and decrypting miscellaneous things but you can add whatever you want to it.
Then, to get the data all you need to do is:
'To get the ConnectionString Key
Dim cn As New SqlConnection(ConfigurationSettings.AppSettings("ConnectString"))
'To reference the Cipher key
ConfigurationSettings.AppSettings("Cipher")
'To reference the DefaultDB key
_DefaultDB = ConfigurationSettings.AppSettings("DefaultDB")
| Doing this stores the values in an XML file and you can edit them with Notepad or just about anything else. This method also gives your app the ability to scale b/c other Operating Systems can read XML a heck of a lot better than reading the registry (which may or may not exist). Granted this isn't a big thing, after all there are still .ini files and the like, but you've got to admit, this is a clean and easy way to get your configuration settings.
|