Save and Get a Registry Value in 'HKEY_LOCAL_MACHINE\Software' using VB.NET - vb.net

In my VB.NET project I want to save and get a registry value in "HKEY_LOCAL_MACHINE\Software" but I am only able to save and get it in "HKEY_LOCAL_MACHINE" but not software.
Here is my code:
For setting the value
My.Computer.Registry.LocalMachine.SetValue("Study", "1")
For getting the value
Dim RegistryCheck As String = My.Computer.Registry.LocalMachine.GetValue("Study")

You have to verify if you have the permission to write (and also read) from HKEY_LOCAL_MACHINE. If you can't get the required permissions (which is quite possible), opt for HKEY_CURRENT_USER (My.Computer.Registry.CurrentUser). Anyway, you have to specify what kind of permissions you're requiring: Read/ReadWrite. See the overloads of the OpenSubKey() method. Two of them let's you specify these requirements. If/When you are granted the permission, create the subkey (CreateSubKey()) and then set a key value inside it (with SetValue()), specifying a value type with RegistryValueKind.

Related

How to query registry rights on a key using vb.net

Need to query a specific key in the HKLM hive for security permissions. We do not want to change the permissions, just log them to a file. To start I would be satisfied with using WriteLine or a msgbox to display the permissions on the key. I'm new to VB.net. I can't find an example of querying keys only adding them, modifying permissions. Can RegistryRights.FullControl be used to return True or False? If so, can someone provide a link online that has a good example?
I have searched for hours online and have tried to modify the examples here to only perform a query on a key, but I cannot create a query on registry key permissions. https://learn.microsoft.com/en-us/dotnet/api/system.security.accesscontrol.registryrights?view=netframework-4.8
Here is a sample of code to attempt permission changes. But we want to query, not change permissions.
' Prevent the current user from writing or changing the
' permission set of the key. Note that if Delete permission
' were not allowed in the previous access rule, denying
' WriteKey permission would prevent the user from deleting the
' key.
rs.AddAccessRule(New RegistryAccessRule(user,
RegistryRights.WriteKey Or RegistryRights.ChangePermissions,
InheritanceFlags.None, PropagationFlags.None, AccessControlType.Deny))
Expected output would be something like this where the field is True or false or 983103 or some other value:
Console.WriteLine("Regkey rights on HKLM\Software\wow6432node\somekey\ " & RegistryRights.FullControl)
In case someone is trying to do the same thing, I found this Sysinternals Application that does exactly what we need to do.
https://learn.microsoft.com/en-us/sysinternals/downloads/accesschk
We'll include it with our app and call it.
Sample command we'll be using
accesschk.exe -k users hklm\software\wow6432node\somekeyhere

How to read values dynamically from a file for a property in updateAttribute?

I added some custom properties in the 'updateAttribute' processor using the '+' button. For example: I declared a property 'DBConnectionURL' and gave the value as 'jdbc:mysql://localhost:3306/test'. Then, in the 'DBCPConnectionPool' service controller, I simple used the value'${DBConnectionURL}' for 'Database Connection URL' property. But, I manually gave the value for 'DBConnectionURL' property.I want a way where I can feed the value dynamically from a file, so that i just need to change the value in the file and the value for 'DBConnectionURL' changes dynamically based on the value present in the file. Is there a way to do it?
Rishab,
You have to use nifi variable registry.
In conf/nifi.properties, you could configure the below configuration in it for dynamically update a value in your data flow.
nifi.variable.registry.properties=./dynamic.properties
You can give your variables in that file dynamic.properties it should present in conf directory.
For an example, If dynamic.properties files contains below values
DBCPURL= jdbc://<host>:<port>
you can use that in your data flow by using ${DBCPURL}
Note: You should restart nifi services if you change any configuration in conf/nifi.properties.Otherwise your changes not worked in dataflow.
Feel free to accept it be answer if it worked for you.

Can't read key?

Ok so I am making this system information application that runs on the desktop, but for some reason this gives me an error.
Error: [ArgumentException was unhandled] Registry key name must start with a valid base key name.
I can see the registry is there.
Dim readValue As String
readValue = My.Computer.Registry.GetValue _
("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\SYSTEM\CentralProcessor\3","ProcessorNameString",Nothing)
Label9.Text = CStr(readValue)
Edit: This sight just helped me, there was 2 Unicode chars in there.
I think the problem with program permission ,HKEY_LOCAL_MACHINE needs administrator access, in app.manifest
Change
to

SSIS Connection Error - File name not valid

I'm seeing an issue with an SSIS (SQL Server 2005) job where I'm getting the following error:
The file name "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\UNC\FOLDERS\filename.xls;Extended Properties="EXCEL 8.0;HDR=YES";" specified in the connection was not valid.
My searching around this site and others indicates that the most common cause of this is a permissions error but I don't believe that's the case in this situation since any number of files have successfully been processed through this implementation.
Here's an overview of the setup:
Vendors FTP files to us on a daily basis that a Windows service picks up, copies to a temporary directory and then calls SSIS jobs on those files. There are two SSIS jobs for each vendor one for a snapshot data feed and one for a transaction listing.
There are currently over 50 different SSIS jobs in the overall process. All of them work except for one specific transaction job which fails with the above error in a script task step. Files come in at least daily with unique file names so I grab the job, determine the vendor based off the source directory and then the file type based off indicators in the file name to determine which SSIS job to call. Since file names change every day, when the service calls the SSIS job, I pass in a series of parameters including the vendor file name so it can properly connect to the file.
Each job begins with a script task that sets necessary variable values for the rest of the job. For example, since the vendor file name changes with each run, I pass in the vendor file name through the SSIS variables collection then set the connection string of a datasource using that file name as the DataSource in the string. It is at that point of the script task that the above error occurs. Here's the task script code where the error occurs:
Dts.Connections("Transactions File").ConnectionString = _
Dts.Variables("ConnectionString").Value.ToString().Replace("##FILE_PATH##", sourceFilePath)
The ConnectionString value is: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=##FILE_PATH##;Extended Properties="EXCEL 8.0;HDR=YES";
The sourceFilePath is the full UNC path to the vendor file in the processing directory
I don't believe it's a permissions error since all the other files going through this process (using the same holding directory for processing) are working. It shouldn't be an issue of the file not existing since again it follows the same process as every other file and I have verified the file properly ends up in the correct directory. I also considered that the connection string might be too long, but the filepath ends up at 109 characters and even with a shorter (<90) full path, the same error occurs.
Is there anything else you can you think of for me to look at? Thanks for any help.
Based on the information presented, you are doing everything correct. If you're new to SSIS, one thing I'd suggest, is that you get a copy of the excellent add-in BIDSHelper. It has great features that can really save you time especially with regard to configurations and expressions.
I created a reference package that had an Excel Connection Manager pointing to C:\ssisdata\so_paulsmithjr.xls and wired everything up.
At this point, I know things are working so it was time to make the package move. I created the following variables and their values
CurrentFile - C:\ssisdata\so_paulsmithjr.xls
PlaceHolder - ##FILE_PATH##
TemplateConnection - Provider=Microsoft.Jet.OLEDB.4.0;Data Source=##FILE_PATH##;Extended Properties="Excel 8.0;HDR=YES";
A fourth variable is set to be an expression (Right click on variable, properties window. Set Evaluate as Expression = True & Expression is below)
CurrentConnection - REPLACE(#[User::TemplateConnection], #[User::PlaceHolder], #[User::CurrentFile])
I compared the CurrentConnection value to the ReferenceConnection (which is the original value of the Excel Connection Manager's connection string) and things were a match. At this point, if I were to change the value of CurrentFile to C:\ssisdata\so_paulsmithjr - Copy.xls, that would automatically be reflected in the value of CurrentConnection.
The final trick would be to use an Expression on the Excel Connection Manager. Again, right click on the CM and under Properties, there will be Expressions. It won't expand as there is nothing under it. Instead click the ellipses and then select ConnectionString property and select the ellipses again and this time drag down the #[User::CurrentFile] variable. Click OK x2 and now your connection manager is set to use wherever the CurrentConnection variable specifies.
Does that work any better?

Registry key error if the folder doesn't exist in VB

I am trying to add a registry key in to the file "MyApp" which doesn't yet exist in the registry key directory, when I try and write this key however I get an error from the debug console telling me the "NullReference was unhanded". If I go and manually make this folder it works brilliantly, so can anyone help me as I thought that this code would make the folder as well? If you could show me what code needs to also be there I would be very grateful!
The code I am using is:
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\MyApp", True).SetValue("AppName", "SerialKey")
OpenSubKey("SOFTWARE\MyApp", True) will open the key for write access only if it already exists. If the key doesn't exist, it will return null. Since you are blindly calling SetValue on a null reference, you will get the null reference exception.
The simplest solution to your requirement is to call CreateSubKey("SOFTWARE\MyApp") instead. This does exactly what you want, i.e. creates a new subkey or opens the existing subkey for write access.
adding and editing LocalMachine registry key needs administrator privilege in win 7, may be you are getting the error because of this