Edit a Registry Value

I am in the process of trying to change a specific registry value, but I keep getting a “RegistryAccessErrorException”.

Here is the code that I have:

if reg.Value(“WriteProtect”) = 0 Then
try
reg.Value(“WriteProtect”) = 1
Label1.Text = “USB Write Blocking Is Now ON”
end try
end if

Any ideas?

How are you constructing the reg variable? Which line raises the exception?

Reg variable:
dim reg as new RegistryItem(“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\StorageDevicePolicies”, TRUE)

The line that throws the error:
try
reg.Value(“WriteProtect”) = 1

Your app needs to be run with full administrative privileges in order to modify registry values within the HKEY_LOCAL_MACHINE hive.

Oh? How do I go about doing that?

Nice Avatar by the way. Calvin rocks!

Hi Andrew,

Disregard. I was thinking of Admin rights with the Xojo build. I understand it is as Admin when the app is run.

Have a good weekend.

To force admin rights right-click the executable and select “Run as administrator”. For debugging, run the IDE as administrator and your app should inherit the privileges.

To permanently mark your finished EXE as needing admin rights, you will need to edit the manifest using a third-party tool (e.g. Resource Hacker [limited but free] or PE Explorer [powerful but not free].)

The manifest is XML data which is embedded in the EXE. Using the third-party tool, edit your finished EXE’s manifest as follows:

Locate this section:

<requestedExecutionLevel
level="asInvoker"
uiAccess="false"/>

And change the “level” value to “requireAdministrator”

Note that it is not possible to edit the manifest in 64 bit builds, because there is none inside. Better stick to classic 32 bits apps for the moment.