Checking The Registry

  • 14 years ago

    I am using VB.NET 2005.

    I wish to write an If statement to determine whether or not a key (in this case: Setting) exists.

    If Setting exists Then

    score = GetSetting(appname:="My_Application", section:="Test", Key:="Setting", Default:="")


    SaveSetting "My_Application", "Test", "Setting", txtInput.Text

    End If

    What do I need to do to check the registry to see whether or not the above statement is true or false?

  • 14 years ago
    Don't use GetSetting and SaveSetting at all in VB 2005.  You should be using application settings for that type of thing.  I suggest that you search for My.Settings in the MSDN library for more information.

  • 14 years ago

    just outta curiousity, what's wrong with SaveSetting/GetSetting in .NET? or if there is a problem with it in all versions?

  • 14 years ago
    GetSetting, SaveSetting and DeleteSetting were the VB6 method of saving application- and user-specific data in the registry.  The standard is for .NET applications to avoid the registry if possible and use other means of persisting data.  The most favoured method is XML, and .NET 2.0 has beefed up the application config file and Framework support for its access.  You should use the Settings tab of the project properties to create your own settings and then access them at run time through the My.Settings object.  These settings are stored in standard .NET file that has the same name as your executable with an additional ".config" extension.  You don't have to know anything about the file though because it is created and accessed automatically.  You can also bind the properties of components and controls created at design time to these settings so the control properties will be automatically loaded at startup and saved at shutdown.  Application settings are not a panacea but they are very useful and should definitely be used in preference to GetSetting and SaveSetting.

  • 14 years ago

    So I have now changed my code to this:


    Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    If CustomerDocument = "1" Then

    My.Settings.InvoiceNum = My.Settings.InvoiceNum + 1

    Label8.Text = "IMON"

    Label10.Text = My.Settings.InvoiceNum


    My.Settings.QuoteNum = My.Settings.QuoteNum + 1

    Label8.Text = "QMON"

    Label10.Text = My.Settings.QuoteNum

    End If

    End Sub

    I dont have to do anything special to save the new number I am making? According to your last post it will autosave when the app closes?

  • 14 years ago

    ok, the above code does not work entirely as I wanted it to.

    First I check to see if CustomerDocument is equal to 1

    if yes then

    Increase the value of Setting InvoiceNum by 1
    display answer in label10.text

    Increase the value of Setting QuoteNum by 1
    display answer in label10.text
    End if

    That all works great and everything, as both settings start off at 0 and get changed to 1. I need to save that new number so that every time my Form_Load event triggers I get a new number and be able to save the last used number.

    The whole point of this is to create a new Invoice number as I deal with new customers and so I can keep track of things.

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian Kernighan