Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello Friends,

I converted my VB6 code to VB.net and due to environment dependencies i have to stick to dotnet 2.0.

Now after converting there is only one piece of code that is failing which i am not able to fix.

Here is the code that fails and gives System.Io.exception: Bad File name or number:

<pre lang="vb">
Dim objShell As Object
Dim strPath As String
Dim strDirectory As String
strPathInstall = "HKEY_LOCAL_MACHINE\SOFTWARE\ABC\InstallInfo\"
strDrive = "INSTALLDRIVE"
objShell = CreateObject("Wscript.Shell")
strDirectory = objShell.ReadRegistry(strPathInstall & strDrive)


The objShell.ReadRegistry statement creates the System.Io.Exception-Bad File name or number, in case the registry entry is not present on the host machine And that crashes my application. If registry is present then it successfully retrieves the value, else throws exception.

I want to handle this exception, i want it to continue to the next statement if it fails to find the registry item in the hostmachine or simply set strDirectory = ""

I cant give try catch to this statement because there are plenty of this readregistry statements in the code and its not good to give try catch to each and every readregistry statement.

is there any way i can fix this problem? Please suggest.

What I have tried:

I tried the dotnet approach, but in certain other scenarios the dotnet approach cant be implemented due to the existing logic.
Posted
Updated 5-Feb-19 22:02pm
v2

MSDN documentation is here: Reading from and Writing to the Registry (Visual Basic) | Microsoft Docs[^] It contains information about security:

Quote:
Robust Programming

The registry holds top-level, or root, keys that are used to store data. For instance, the HKEY_LOCAL_MACHINE root key is used for storing machine-level settings used by all users, while HKEY_CURRENT_USER is used for storing data specific to an individual user.

The following conditions may cause an exception:

The name of the key is Nothing (ArgumentNullException).

The user does not have permissions to read from registry keys (SecurityException).

The key name exceeds the 255-character limit (ArgumentException).

.NET Framework Security

To run this process, your assembly requires a privilege level granted by the RegistryPermission class. If you are running in a partial-trust context, the process might throw an exception due to insufficient privileges. Similarly, the user must have the correct ACLs for creating or writing to settings. For example, a local application that has the code access security permission might not have operating system permission. For more information, see Code Access Security Basics[^].


Note: even if your code is working (or seems to be), it doesn't mean that it's a 100% .net version. I'd recommend to write your application from scratch. In a process of creation you have to keep in mind that VB.NET is not VB. The differences you'll find here: Comparison of Visual Basic and Visual Basic .NET - Wikipedia[^]

Note #2 i'd avoid of keeping information in a registry (unless it's necessary). There's a lot of places where you can store application/user data. Please see: Where should I store my data?[^]
 
Share this answer
 
Sometimes Google helps :
[^]
Perhaps you try it like this ...
 
Share this answer
 
Comments
Dinesh Kumar Dora 31-Jan-19 7:01am    
Thanks for the response. As mentioned earlier, i already tried dotnet direct approach to read registry but there are certain areas in this legacy code where i cant use the dotnet approach directly. Is there a way where i can handle the existing objShell.ReadRegistry in a better way. I am currently thinking of catching the system.io.exceptions and use on error resume next in a controlled way. As its just about registry reading, nothing much, so i think i can skip certain errors in specific locations.
Dave Kreskowiak 31-Jan-19 8:04am    
Can't use the .NET classes to do registry? Why do you think that is?
Ralf Meier 31-Jan-19 8:45am    
If you work with .Net you should ALLWAYS use it's internal functionality ...
As you allready have seen - the VBA functionality doesn't work or doesn't work correct ...
Maciej Los 6-Feb-19 4:03am    
5ed!
Ralf Meier 6-Feb-19 7:10am    
... and again Thanks for your vote ... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900