Как отобразить значение InstallPath из реестра в консоли?

Рейтинг: 1Ответов: 1Опубликовано: 05.05.2015

Ошибка

Нужно, чтобы значение InstallPath отображалось в консоли.

Что я делаю не так?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.IO;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            RegistryKey hklm = Registry.LocalMachine;
            RegistryKey hksf = hklm.OpenSubKey("SOFTWARE");
            RegistryKey hkvl = hksf.OpenSubKey("Valve\\Steam");
            object readypath = hkvl.GetValue("InstallPath");
            readypath.ToString();
            hkvl.Close();
            Console.WriteLine(readypath);
        }
    }
}

Ответы

▲ 1Принят

Чтобы задать 32-битное представление реестра на 64-битной системе, используйте RegistryView.Registry32. Т.е. Ваш код будет выглядеть как-то так

static void Main(string[] args)
    {
        using (var lmRegistry = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32))
        {
            var steamRegistry = lmRegistry.OpenSubKey(@"SOFTWARE\Valve\Steam", false);
            string readypath = steamRegistry.GetValue("InstallPath").ToString();
            Console.WriteLine(readypath);
        }
    }
}

Подсмотрено из Reading the registry and Wow6432Node key - Stack Overflow