0

I have written a C# code to find that particular version is installed in a machine or not. When i test this codeby debugging, it works as i expected.

But, when i pass the arguments and run the asserts through N-unit, am getting the wrong output. I suspect that N-Unit is not accessing the registry entry.

Could anyone please provide your suggestions on this and guide me to go with correct path?

Following is my N-Unit code

[Test]
        public void IsSTDInstalled()
        {
            Assert.IsTrue(GetInstalledDetail.IsInstalled("1.4.0.2"));

            Assert.IsFalse(GetInstalledDetail.IsInstalled("1.4.0.3"));

        }

Here is the IsInstalled method code:

public static bool IsInstalled(string version)
        {
            string path = string.Empty;
            bool result = false;
            string sLocation = "SOFTWARE\\A\\InstalledVersions";
            string sLocation64bit = "SOFTWARE\\Wow6432Node\\A\\InstalledVersions";
            RegistryKey key, versionKey;
            key = Registry.LocalMachine.OpenSubKey(sLocation);
            if (key == null)
            {
                key = Registry.LocalMachine.OpenSubKey(sLocation64bit);
                sLocation = sLocation64bit;
            }
            if (key != null)
            {
                foreach (string isVersion in key.GetSubKeyNames())
                {
                    if (isVersion == version)
                    {
                        versionKey = Registry.LocalMachine.OpenSubKey(sLocation + "\\" + version);
                        try
                        {
                            path = versionKey.GetValue("") as string;
                            if (Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).Any())
                            {
                                result = true;
                                break;
                            }
                            else
                            {
                                result = false;
                            }
                        }
                        catch (Exception exception)
                        {
                            result = false;
                        }
                    }
                }
            }
            return result;
        }

Thanks in advance!!!

Karthi
  • 575
  • 2
  • 10
  • 29

0 Answers0