예제 #1
0
        /// <summary>
        /// Gets the value of a registry key in HKEY_LOCAL_MACHINE.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path">A path relative to HKEY_LOCAL_MACHINE.  E.g. "SOFTWARE\\Microsoft"</param>
        /// <param name="key">Key</param>
        /// <param name="defaultValue">Value to return if the key does not exist.</param>
        /// <returns></returns>
        public static T GetHKLMValue <T>(string path, string key, T defaultValue)
        {
            object value = HKLM.OpenSubKey(path)?.GetValue(key);

            if (value == null)
            {
                return(defaultValue);
            }
            return((T)value);
        }
예제 #2
0
        /// <summary>
        /// Attempts to set the value of the specified registry key, throwing an exception if it fails.
        /// </summary>
        /// <param name="path">Path to the folder where the key is located, relative to HKEY_LOCAL_MACHINE.</param>
        /// <param name="key">Name of the key to set the value of.</param>
        /// <param name="value">Value to set.</param>
        /// <param name="valueKind">The type of value stored in this registry key.</param>
        /// <returns></returns>
        public static void SetHKLMValue(string path, string key, object value, RegistryValueKind valueKind = RegistryValueKind.Unknown)
        {
            RegistryKey sk = HKLM.CreateSubKey(path);

            if (valueKind == RegistryValueKind.Unknown)
            {
                sk.SetValue(key, value);
            }
            sk.SetValue(key, value, valueKind);
        }
예제 #3
0
 /// <summary>
 /// Returns the requested RegistryKey or null if the key does not exist.
 /// </summary>
 /// <param name="path">A path relative to HKEY_LOCAL_MACHINE.  E.g. "SOFTWARE\\Microsoft"</param>
 /// <returns></returns>
 public static RegistryKey GetHKLMKey(string path)
 {
     return(HKLM.OpenSubKey(path));
 }
예제 #4
0
        public static int Main(string[] argv)
        {
            RegistryKey HKLM;
            RegistryKey Key;

            WriteToFile = false;

            // Parse the command line parameters.
            if (argv.Length > 0)
            {
                if (argv[0] == "/?")
                {
                    // Display the help.
                    Console.WriteLine("instsoft version " + VERSION_MAJOR + "." + VERSION_MINOR + ", Copyright (c) 2011, Lucas M. Suggs");
                    Console.WriteLine("Usage: instsoft.exe [/f path] [computername]");

                    return(0);
                }

                if ((argv[0] == "/f") && (argv.Length > 1))
                {
                    // User specified to output to a path rather than stdout.
                    OutputPath = argv[1] + "\\";

                    WriteToFile = true;

                    if (!Directory.Exists(OutputPath))
                    {
                        Console.Error.WriteLine("Invalid output path: " + OutputPath);

                        return(ERROR_INVALID_PATH);
                    }

                    if (argv.Length > 2)
                    {
                        ComputerName = argv[2];
                    }
                    else
                    {
                        ComputerName = Environment.MachineName;
                    }
                }
                else
                {
                    // We don't recognize the first parameter as a flag, so it
                    // is assumed that it is a computer name.
                    ComputerName = argv[0];
                }
            }
            else
            {
                // No command line parameters, so grab the local computer name.
                ComputerName = Environment.MachineName;
            }

            // Figure out the OS architecture of the computer name we are using.
            x64 = GetOsArchitecture(ComputerName);

            if (WriteToFile)
            {
                // Since we are writing to a file, craft the file name.
                string FileName = OutputPath + ComputerName + "_";

                // DateTime.Now.ToString("s") outputs to the format:
                //    2011-12-08T16:01:35
                // So we want to strip the - and : and turn the T into a - so
                // we get:
                //    20111208-160135
                FileName += DateTime.Now.ToString("s").Replace(":", "").Replace("-", "").Replace("T", "-") + ".txt";

                file = File.CreateText(FileName);

                Console.WriteLine(FileName);
            }

            try
            {
                // Attempt to open the registry of the remote computer.
                HKLM = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, ComputerName);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Unable to open the registry:");
                Console.Error.WriteLine(e);

                return(ERROR_BAD_REGISTRY);
            }

            // Set up the software list.
            SoftwareList = new SortedList();

            try
            {
                // This first registry location is the MSI uninstall records.
                // This location contains the most detailed information, but
                // also does not contain a list of all installed software.
                Key = HKLM.OpenSubKey(SoftwareListKey1);

                foreach (string key in Key.GetSubKeyNames())
                {
                    RegistryKey SubKey = Key.OpenSubKey(key);

                    QueryKey(SubKey);
                }

                // The second registry location is the MSI install records;
                // however, this location, while having entries that are missing
                // in the MSI uninstall records, does not have install date or
                // program version information for installed software (WTF?).
                Key = HKLM.OpenSubKey(SoftwareListKey2);

                foreach (string key in Key.GetSubKeyNames())
                {
                    RegistryKey SubKey = Key.OpenSubKey(key);

                    QueryKey(SubKey);
                }

                if (x64)
                {
                    // The third registry location is the WoW6432node MSI
                    // uninstall records. This will pull software entries
                    // that were installed with an MSI flagged as 64-bit.
                    //
                    // Due to registry virtualization, this seems to be the only
                    // way to get the opposite architecture's list of installed
                    // software (opposite of the architecture of the currently
                    // running instsoft program).
                    Key = HKLM.OpenSubKey(SoftwareListKey3);

                    foreach (string key in Key.GetSubKeyNames())
                    {
                        RegistryKey SubKey = Key.OpenSubKey(key);

                        QueryKey(SubKey);
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
            }

            // Display the software list.
            DisplaySoftwareList();

            return(0);
        }