Link to home
Start Free TrialLog in
Avatar of pchiappisi
pchiappisi

asked on

Small VB App, really confused.

Hello everyone,
      Working on a small app and i am having some issues getting it to work.    Now let me say right up front i am NOT a programmer and have only worked with VB studio to build/compile code that has already been created for my company.     So i know my way around VB studio but the coding itself is over my head.

My goal is to create a small .exe that will open a window with a button on it, if the button is clicked i want a reg key deleted (HKLM/Software/MyApp/Config/ KEY:DMAC).     sounds simple enough i though to myself.............

I have a basic form and have created a new button (yeah, i know, that's the hard part). I used the "insert snippet" option to select the delete reg key code, the Code attached in the code field indicates i'm missing the "method body" when i go to build the project though.   The code listed is the ONLY code i have for the form, what am i missing?

Looking at the code, it is also not referencing the button anywhere, i can't figure out how to make that link.

It would also be good if clicking the button also disabled the .exe itself so that it was only runnable once per machine (by storing a .reg key woudl be acceptable).

Would be awesome if someone was bored enough to slap such an app together for me but any help at all woudl be great.

Much thanks in advance!
Phil C.
Imports Microsoft.Win32
Using key As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("Software\MyApp\Config")
    key.DeleteSubKey("DMAC")
End Using

Open in new window

Avatar of Michael_D
Michael_D
Flag of Canada image

if you just double click that button in the designview IDE will create an empty sub for you so you can put your code there

If you want to get realy dirty with coding you can create subroutine by yourself and specify the event you want to respond to using Handles keyword

here is how:

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Your code goes here
    End Sub

hope this helps
Avatar of pchiappisi
pchiappisi

ASKER

great amount of progress thanks to you Michael, i can compile and the button is running the action, i now am getting an error about not being able to access the registry though.   Tried "run as administrator" with the same result.

Assuming i need to be in some sort of privileged install mode.    

System.UnauthorizedAccessException: Cannot write to the registry key.
   at System.ThrowHelper.ThrowUnauthorizedAccessException(ExceptionResource resource)
   at Microsoft.Win32.RegistryKey.EnsureWriteable()
   at Microsoft.Win32.RegistryKey.DeleteSubKey(String subkey, Boolean throwOnMissingSubKey)
   at Microsoft.Win32.RegistryKey.DeleteSubKey(String subkey)
   at WindowsApplication3.Form1.Button1_Click(Object sender, EventArgs e) in C:\Users\phil\Documents\Visual Studio 2005\Projects\DMAC Reset\WindowsApplication3\Form1.vb:line 6
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Are you sure that the key exists?

Anyway, try this code to make it sure

        Try
            If My.Computer.Registry.LocalMachine.OpenSubKey("Software\MyApp\Config") IsNot Nothing Then

                Using key As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("Software")
                    key.DeleteSubKey("DMAC")
                End Using
            Else
                MessageBox.Show("Key does not exist!")

            End If
        Finally
            My.Computer.Registry.LocalMachine.Close()
        End Try
ASKER CERTIFIED SOLUTION
Avatar of Michael_D
Michael_D
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Again, thank you for your help Michael.      I have HKLM/Software/MyApp/Config/DMAC/ and get the following error when i run the app again claiming i don't have access to perform the function (tried again running as administrator with no luck).    

 This is not exactly what i am looking for though, which is my fault for not explaining it correctly, the DMAC entry i want to delete is a string value not a key, sorry.
so what i really want to delete is the REG_SZ value "DMAC" under "HKLM/Software/MyApp/Config/

soooooo close, any other ideas?

************** Exception Text **************
System.UnauthorizedAccessException: Attempted to perform an unauthorized operation.
   at Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str)
   at Microsoft.Win32.RegistryKey.DeleteSubKey(String subkey, Boolean throwOnMissingSubKey)
   at Microsoft.Win32.RegistryKey.DeleteSubKey(String subkey)
   at WindowsApplication3.Form1.Button1_Click(Object sender, EventArgs e) in C:\Users\phillip\Documents\Visual Studio 2005\Projects\DMAC Reset\WindowsApplication3\Form1.vb:line 11
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

OK, got it working when i "Run as administrator" is there anyway around that?   not the end of the world.