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/Confi g/ 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.
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/Confi
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
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.UnauthorizedAccessE xception: Cannot write to the registry key.
at System.ThrowHelper.ThrowUn authorized AccessExce ption(Exce ptionResou rce resource)
at Microsoft.Win32.RegistryKe y.EnsureWr iteable()
at Microsoft.Win32.RegistryKe y.DeleteSu bKey(Strin g subkey, Boolean throwOnMissingSubKey)
at Microsoft.Win32.RegistryKe y.DeleteSu bKey(Strin g subkey)
at WindowsApplication3.Form1. Button1_Cl ick(Object sender, EventArgs e) in C:\Users\phil\Documents\Vi sual Studio 2005\Projects\DMAC Reset\WindowsApplication3\ Form1.vb:l ine 6
at System.Windows.Forms.Contr ol.OnClick (EventArgs e)
at System.Windows.Forms.Butto n.OnClick( EventArgs e)
at System.Windows.Forms.Butto n.OnMouseU p(MouseEve ntArgs mevent)
at System.Windows.Forms.Contr ol.WmMouse Up(Message & m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Contr ol.WndProc (Message& m)
at System.Windows.Forms.Butto nBase.WndP roc(Messag e& m)
at System.Windows.Forms.Butto n.WndProc( Message& m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.OnMessa ge(Message & m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.WndProc (Message& m)
at System.Windows.Forms.Nativ eWindow.Ca llback(Int Ptr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Assuming i need to be in some sort of privileged install mode.
System.UnauthorizedAccessE
at System.ThrowHelper.ThrowUn
at Microsoft.Win32.RegistryKe
at Microsoft.Win32.RegistryKe
at Microsoft.Win32.RegistryKe
at WindowsApplication3.Form1.
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Nativ
Are you sure that the key exists?
Anyway, try this code to make it sure
Try
If My.Computer.Registry.Local Machine.Op enSubKey(" Software\M yApp\Confi g") IsNot Nothing Then
Using key As RegistryKey = My.Computer.Registry.Local Machine.Op enSubKey(" Software")
key.DeleteSubKey("DMAC")
End Using
Else
MessageBox.Show("Key does not exist!")
End If
Finally
My.Computer.Registry.Local Machine.Cl ose()
End Try
Anyway, try this code to make it sure
Try
If My.Computer.Registry.Local
Using key As RegistryKey = My.Computer.Registry.Local
key.DeleteSubKey("DMAC")
End Using
Else
MessageBox.Show("Key does not exist!")
End If
Finally
My.Computer.Registry.Local
End Try
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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/Confi g/
soooooo close, any other ideas?
************** Exception Text **************
System.UnauthorizedAccessE xception: Attempted to perform an unauthorized operation.
at Microsoft.Win32.RegistryKe y.Win32Err or(Int32 errorCode, String str)
at Microsoft.Win32.RegistryKe y.DeleteSu bKey(Strin g subkey, Boolean throwOnMissingSubKey)
at Microsoft.Win32.RegistryKe y.DeleteSu bKey(Strin g subkey)
at WindowsApplication3.Form1. Button1_Cl ick(Object sender, EventArgs e) in C:\Users\phillip\Documents \Visual Studio 2005\Projects\DMAC Reset\WindowsApplication3\ Form1.vb:l ine 11
at System.Windows.Forms.Contr ol.OnClick (EventArgs e)
at System.Windows.Forms.Butto n.OnClick( EventArgs e)
at System.Windows.Forms.Butto n.OnMouseU p(MouseEve ntArgs mevent)
at System.Windows.Forms.Contr ol.WmMouse Up(Message & m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Contr ol.WndProc (Message& m)
at System.Windows.Forms.Butto nBase.WndP roc(Messag e& m)
at System.Windows.Forms.Butto n.WndProc( Message& m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.OnMessa ge(Message & m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.WndProc (Message& m)
at System.Windows.Forms.Nativ eWindow.Ca llback(Int Ptr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
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/Confi
soooooo close, any other ideas?
************** Exception Text **************
System.UnauthorizedAccessE
at Microsoft.Win32.RegistryKe
at Microsoft.Win32.RegistryKe
at Microsoft.Win32.RegistryKe
at WindowsApplication3.Form1.
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Nativ
ASKER
OK, got it working when i "Run as administrator" is there anyway around that? not the end of the world.
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