9 Replies - 7300 Views - Last Post: 22 January 2011 - 12:16 PM Rate Topic: -----

#1 sprayReborn   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 37
  • Joined: 19-October 10

For each setting loop

Posted 20 January 2011 - 07:03 PM

Hi. I am trying to make a program that deletes all setting made by using the SaveSetting function. I don't use the MySettings class as it makes a setting for each user. This is basically what I need it to do:

For Each setting
DeleteSetting(setting)
Loop



I would really appreciate the help! Thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: For each setting loop

#2 Shadar   User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: For each setting loop

Posted 21 January 2011 - 07:32 AM

I do hope you realize your playing with fire. Most For Each loops, loop through a collection of something, records, controls, array items etc. You are playing with the keys of the registry. The registry is a group of record stored in what is called a hive.
If you want to learn more about how the registry works take a look at this:

http://support.microsoft.com/kb/256986

The bottom line is I would seriously think twice before looping through registry keys with a delete command of any kind.

Having said that, if you are still determined to try it, I will point you in the right direction.

Take a look at the Microsoft.Win32.Registry and Microsoft.Win32.RegistryKey classes.

MSDN: The following code example demonstrates how to retrieve the subkeys of the HKEY_USERS key, and print their names to the screen. Use the OpenSubKey method to create an instance of the particular subkey of interest. You can then use other operations in RegistryKey to manipulate that key.
Imports System
Imports Microsoft.Win32

Class Reg

    Public Shared Sub Main()
        
        ' Create a RegistryKey, which will access the HKEY_USERS
        ' key in the registry of this machine.
        Dim rk As RegistryKey = Registry.Users
        
        ' Print out the keys.
        PrintKeys(rk)
    End Sub    
    
    Shared Sub PrintKeys(rkey As RegistryKey)
        
        ' Retrieve all the subkeys for the specified key.
        Dim names As String() = rkey.GetSubKeyNames()
        
        Dim icount As Integer = 0
        
        Console.WriteLine("Subkeys of " & rkey.Name)
        Console.WriteLine("-----------------------------------------------")
        
        ' Print the contents of the array to the console.
        Dim s As String
        For Each s In  names
            Console.WriteLine(s)
            
            ' The following code puts a limit on the number
            ' of keys displayed.  Comment it out to print the
            ' complete list.
            icount += 1            
            If icount >= 10 Then
                Exit For
            End If
        Next s
    End Sub
End Class


Another word of caution (I speak from experience) make sure your code is doing what you think it should be doing BEFORE you run your code.

This post has been edited by Shadar: 21 January 2011 - 08:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 Jack Eagles1   User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 187
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: For each setting loop

Posted 21 January 2011 - 11:20 AM

Edit: I was wrong, having looked at the OP, my advice did not apply. Thanks for the heads up Shadar.

This post has been edited by Jack Eagles1: 21 January 2011 - 12:06 PM

Was This Post Helpful? 0
  • +
  • -

#4 Shadar   User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: For each setting loop

Posted 21 January 2011 - 11:58 AM

View PostsprayReborn, on 20 January 2011 - 08:03 PM, said:

Hi. I am trying to make a program that deletes all setting made by using the SaveSetting function. I don't use the MySettings class as it makes a setting for each user. This is basically what I need it to do:


I don't know. He mentions "SaveSetting" which indicates registry, instead of my.Settings.Save. Then says he isn't using MySettings.

I learned the hard way about playing with the registry through code. Thank God for Norton Ghost.
Was This Post Helpful? 1
  • +
  • -

#5 Shadar   User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: For each setting loop

Posted 21 January 2011 - 12:42 PM

View PostJack Eagles1, on 21 January 2011 - 12:20 PM, said:

Edit: I was wrong, having looked at the OP, my advice did not apply. Thanks for the heads up Shadar.



I played with the My.Mysettings and figured out that you can loop through the mysettings to get the Name of your setting and value.

For Each CSPV As Configuration.SettingsPropertyValue In My.Settings.PropertyValues
   MessageBox.Show(CSPV.Name & " - " & CSPV.PropertyValue.ToString)
Next


This post has been edited by Shadar: 21 January 2011 - 01:36 PM

Was This Post Helpful? 0
  • +
  • -

#6 sprayReborn   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 37
  • Joined: 19-October 10

Re: For each setting loop

Posted 21 January 2011 - 01:51 PM

View PostShadar, on 21 January 2011 - 12:42 PM, said:

View PostJack Eagles1, on 21 January 2011 - 12:20 PM, said:

Edit: I was wrong, having looked at the OP, my advice did not apply. Thanks for the heads up Shadar.



I played with the My.Mysettings and figured out that you can loop through the mysettings to get the Name of your setting and value.

For Each CSPV As Configuration.SettingsPropertyValue In My.Settings.PropertyValues
   MessageBox.Show(CSPV.Name & " - " & CSPV.PropertyValue.ToString)
Next



That doesn't work. It only shows settings pre-made by the coder. Look at the "SaveSetting" and "GetSetting" function.
Was This Post Helpful? -1
  • +
  • -

#7 Shadar   User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: For each setting loop

Posted 21 January 2011 - 02:52 PM

Look at my message at Posted Today, 08:32 AM

Look at the code. It is a console application that shows you how to loop through the registry settings. I am not going to do it for you but this shows you a starting point and more or less how to do it. The code above that you quoted was from a discussion earlier with Jack on My.Settings.

From your original post, you are working with the registry and not my settings. The original message from 08:32 AM gives you enough to figure it out.

This post has been edited by Shadar: 21 January 2011 - 03:23 PM

Was This Post Helpful? 0
  • +
  • -

#8 sprayReborn   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 37
  • Joined: 19-October 10

Re: For each setting loop

Posted 21 January 2011 - 04:00 PM

View PostShadar, on 21 January 2011 - 02:52 PM, said:

Look at my message at Posted Today, 08:32 AM

Look at the code. It is a console application that shows you how to loop through the registry settings. I am not going to do it for you but this shows you a starting point and more or less how to do it. The code above that you quoted was from a discussion earlier with Jack on My.Settings.

From your original post, you are working with the registry and not my settings. The original message from 08:32 AM gives you enough to figure it out.

Ok. If anyone would listen to me, I am using the BUILT IN fuction SaveSetting. It doesn't have anything to do with the registry. It may but the whole point of it is so we don't have to deal with the registry.
Was This Post Helpful? 0
  • +
  • -

#9 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: For each setting loop

Posted 21 January 2011 - 05:20 PM

Do you mean something like this, which does use the registry:

Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.FormClosing
     ' Save the value to the registry
      SaveSetting(Application.ProductName, Application.ProductName, "CheckBox1", CheckBox1.Checked)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     ' Get the value
      CheckBox1.Checked = GetSetting(Application.ProductName, Application.ProductName, "CheckBox1")
End Sub


Personally, I write my own with and xmldocument.
Was This Post Helpful? 0
  • +
  • -

#10 sprayReborn   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 37
  • Joined: 19-October 10

Re: For each setting loop

Posted 22 January 2011 - 12:16 PM

Okay, I've finally made it do what I need it to do, it just doesn't work? Know what I'm saying? I have another problem now. When I try to edit the registry, nothing happens. It doesn't call any exceptions, and nothing shows up in the immediate window, but when I look in the registry editor nothing is there. Take a look at my source code for a login system. Can anyone help me figure out why it doesn't work? Thanks.

EDIT: On form2, the "Create Account" button does this:
        If TextBox2.Text = TextBox3.Text Then
            Dim regKey As RegistryKey
            Dim keyValue As String
            keyValue = "SOFTWARE\FileBank\" + TextBox1.Text
            regKey = Registry.LocalMachine.OpenSubKey(keyValue, False)


            If regKey Is Nothing Then
                MsgBox("This username is already in use. Please try a different one.", MsgBoxStyle.Critical, "Error!")
            Else
               createAccount()
            End If

        Else
            MsgBox("Passwords do not match!", MsgBoxStyle.Critical, "Error!")

        End If
        Exit Sub
placeholdeR:
        createAccount()


But it should do this:
        If TextBox2.Text = TextBox3.Text Then
            Dim regKey As RegistryKey
            Dim keyValue As String
            keyValue = "SOFTWARE\FileBank\" + TextBox1.Text
            regKey = Registry.LocalMachine.OpenSubKey(keyValue, False)


            If regKey Is Nothing Then
                createAccount()
            Else
                MsgBox("This username is already in use. Please try a different one.", MsgBoxStyle.Critical, "Error!")
            End If

        Else
            MsgBox("Passwords do not match!", MsgBoxStyle.Critical, "Error!")

        End If


Also, this was made in Visual Studio 2010, so I don't know if it will load in anything older.

Attached File(s)


This post has been edited by sprayReborn: 22 January 2011 - 12:22 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1