Check if Access 2010 installed gives exeption

user9250176

I'm trying to check if Access 2010 installed with C#, I tried to use This answer.

He use there subString instead of Substring also with indexOf instead of IndexOf.

So in my code i done it with Substringand IndexOf, but when I run it gives FormatException, here is my code:

RegistryKey rootKey = Registry.ClassesRoot.OpenSubKey(@"Access.Application\CurVer" , false);

if (rootKey == null)
{     
    MessageBox.Show("Access 2010 not installed on this machine");
}

String value = rootKey.GetValue("").ToString();
int verNum = 0;
try
{
    verNum = int.Parse(value.Substring(value.IndexOf("Access.Application.")));
} catch (FormatException fe)
{
    MessageBox.Show(fe.ToString());
}

if (value.StartsWith("Access.Application.") && verNum >= 12)
{       
    MessageBox.Show("Access 2010 already installed on this machine");
}
TheGeneral

There is no way on earth what you have is going to work (just saying)

You have obviously obtained this code from here or some derivative Check if MS Access 2010 is installed... And its horribly wrong

Firstly

string.IndexOf

Reports the zero-based index of the first occurrence of the specified string in this instance

Meaning its going to return 0 if it finds "Access.Application."

Secondly

String.Substring

Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.

Which means, given 0 will return "Access.Application." and that is not an int

Lastly

Int32.Parse

Throws an exception if its not an int


Im not sure of the correct way to find the access version number or how to detect if access is installed. However, if the version number truly lives behind "Access.Application." you'd want to use String.LastIndexOf Method passing in .

At the very least use int.TryParse to make sure it doesnt throw an exception

Example

var somekey = "Access.Application.2099";
var lastIndex = somekey.LastIndexOf(".");

if (lastIndex > 0)
   Console.WriteLine("We have a chance");

var substr = somekey.Substring(lastIndex + 1);

Console.WriteLine(substr);

int verNum = 0;

if (int.TryParse(substr, out verNum))
{
   Console.WriteLine("found a version maybe : " + verNum);
}
else
{
   Console.WriteLine("No cigar");
}

Demo here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related