[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Small VB App, really confused.

Posted on 2009-04-27
6
Medium Priority
?
470 Views
Last Modified: 2013-12-26
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

0
Comment
Question by:pchiappisi
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:Michael_D
ID: 24246076
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
0
 

Author Comment

by:pchiappisi
ID: 24246448
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)
0
 
LVL 13

Expert Comment

by:Michael_D
ID: 24246517
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 13

Accepted Solution

by:
Michael_D earned 500 total points
ID: 24246523
oh I see what the problem. disregard my previous comment and try this
        Try
            If My.Computer.Registry.LocalMachine.OpenSubKey("Software\MyApp\Config") IsNot Nothing Then
 
                Using key As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("Software\MyApp\Config", True)
                    key.DeleteSubKey("DMAC")
                End Using
            Else
                MessageBox.Show("Key does not exist!")
 
            End If
        Finally
            My.Computer.Registry.LocalMachine.Close()
        End Try

Open in new window

0
 

Author Comment

by:pchiappisi
ID: 24251269
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)

0
 

Author Comment

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


0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question