I got this problem while trying to disable or enable ports. It works well when Im using my friend's laptop but when I tried using my laptop, it give this error. Why was this happen and how to solve it? Thanks ^^

Private Sub DisableBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisableBtn.Click
        Microsoft.Win32.Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 4, Microsoft.Win32.RegistryValueKind.DWord) 'this line gives error
        MessageBox.Show("Port are disable", "USB Disable", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Me.Hide()
        Login.Show()
    End Sub

    Private Sub EnableBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnableBtn.Click
        Microsoft.Win32.Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 3, Microsoft.Win32.RegistryValueKind.DWord) 'this line gives error
        MessageBox.Show("Port are enable", "USB Enable", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Me.Hide()
        Login.Show()
    End Sub

Here's the thing. Windows 95 and forward has that registry so not much hasn't been written but I'll sum it up to one word. "Permissions."

Just like a file system with permissions the registry entries can have permissions to slow/stop/block changes. You can start a debate over whether this is secure or not. But skipping past that pothole, you will find permissions on registry items to be sometimes and sometimes not the same.

How you handle that in your code is not a sure thing. To add more to this some security suites also intercept registry changes.

There is no simple cure for this one but you could wrap your code in some try/fail block so you know if it worked or not.

After a failure you get to dive into why it failed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.