• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2099
  • Last Modified:

Changing key/pair value in App.config - vb..net

I have declared a CONNECTION key in app.config. Which I used throught out my application.

Required ?????
Want to have a SETTINGS page (GUI) in my application, which asks for Database name, Server, user id and password.

How can we change values in app.config file, updating the CONNECTION key with what user has entered.

Hope you understand what im looking for.

In a nutshell, you have declared a KEY in your app.config and you want to change it thru an interface.

Regards

Lokesh
0
radhakrishan1
Asked:
radhakrishan1
  • 6
  • 6
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
The class provided by the framework is a read-only access to the config file.

See http://www.west-wind.com/presentations/configurationclass/configurationclass.asp for a complete access to config file that even support encryption (which is welcome with connection parameters).
0
 
radhakrishan1Author Commented:
Above link has code in C#, I have just learning vb.net, so I don't know much about syntax part of c#.

The above link is very good example/code for configuration class.

Have you got the same code in vb.net or can I get some code in vb.net

Thanks alot
0
 
Éric MoreauSenior .Net ConsultantCommented:
I do VB.Net too. I have kept this class as an assembly that I use from my VB applications. You don't need to look at the code if you don't wish.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
radhakrishan1Author Commented:
Means I add the dll file in my VB project and use this dll file.

Can you give me code how you used in your VB.

How you are calling these functions in VB.NET ????????

Code u used in vb.net to modify/set value in config file and encrypting part mentioned in this C# code.

Thanks so much for your advice.
0
 
Éric MoreauSenior .Net ConsultantCommented:
I have this class in my application:

Public Class SYS5AppConfig
    Inherits Westwind.Tools.wwAppConfiguration

    'Your key/pair values
    Public Key1 As String = "Value1"
    Public Key2 As String = "Value2"
    Public Key3 As String = "Value3"

    'Nom du fichier de configuration
    Friend gstrFileName As String = Application.StartupPath & "\YourConfigFile.xml"

    'List encrypted keys
    Private Const mkstrEncryptFields As String = "Key1"
    Private Const mkstrEncryptKey As String = "PutYourEncryptKeyHere"

    Public Sub New()
        Me.SetEncryption(mkstrEncryptFields, mkstrEncryptKey)
        Me.ReadKeysFromConfig(gstrFileName)
    End Sub

End Class


Then you can use this class elsewhere in your application:

Public Shared Configuration As New SYS5AppConfig

Msgbox.show(Configuration.Key1)
0
 
radhakrishan1Author Commented:
Im having problem while running this program downloaded from west wind site.

I have loaded this dll file and when trying to run the code im getting problem here :

foreach(MemberInfo Field in Fields)
                  {
                        // *** If we can't find the key - write it out to the document
                        string Value = null;
                        if (Field.MemberType == MemberTypes.Field)
                              Value = ((FieldInfo) Field).GetValue(this).ToString();

System.null exception is thrown at last line.

Can you provide me the complete working code if you can for this and email me at radhakrishan@hotmail.com

Thanks so much for your help. Request if you could send me working code.

Regards

Lokesh
0
 
radhakrishan1Author Commented:
I have even increased points to 200. Pls help me if you can.
0
 
Éric MoreauSenior .Net ConsultantCommented:
I think I have modified it:

                  foreach(MemberInfo Field in Fields)
                  {
                        // *** If we can't find the key - write it out to the document
                        string Value = null;
                        if (Field.MemberType == MemberTypes.Field)
                              Value = ((FieldInfo) Field).GetValue(this).ToString();
                        else if (Field.MemberType == MemberTypes.Property)
                              Value = ((PropertyInfo) Field).GetValue(this,null).ToString();
                        else
                              continue; // not a property or field


You may also may want to have a look at an article from the VSMag: http://www.fawcette.com/vsm/2004_10/magazine/columns/gettingstarted/
0
 
radhakrishan1Author Commented:
Even I have got the same code, which you just mentioned above.

Again  my problem is solved, im using an xml file to store application settings as you can change it.

Only thing im after now is encryption and decryption.

Link you mentioned above has gon't any code for encryption and decryption of keys.

So can you pls provide me just encryption and decryption of keys part.

Thanks so much for giving your valuable time.
0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
radhakrishan1Author Commented:
Thanks it worked. I downloaded one which you had send me.

Excellent brother.

But still my concept is not clear about this whole application

Can you pls take one example and explain this
    <add key="key1" value="Connection" />

What I need in my application :
1. How to fetch value
2. how to set value
3. how to encrypt value
4. how to decrypt value

Again all using the west-wind dll. Just tell me commands from them as per the west wind.dll

Last help brother and then I will accept it as an anwer.

Thanks so much for your help.


0
 
Éric MoreauSenior .Net ConsultantCommented:
>>my problem is solved, im using an xml file to store application settings as you can change it

So you probably can answer yourself to 1 and 2!

For 3 and 4, you need to know which keys are encrypted and when you read/write to XML, be sure to call the method to crypt your data. Be very cautious as certain characters are not accepted into a XML file. Have a look at the West-Wind class to see how Rick did it!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now