C
C#6mo ago
Remaetanju

Trouble reading registry

string value64 = string.Empty;
string value32 = string.Empty;

const string subkey = @"Test";
const string valueName = "Testvalue";

RegistryKey localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
localKey = localKey.OpenSubKey(subkey);
if (localKey != null)
{
value64 = localKey.GetValue(valueName).ToString();
MessageBox.Show(value64);

}
RegistryKey localKey32 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
localKey32 = localKey32.OpenSubKey(subkey);
if (localKey32 != null)
{
value32 = localKey32.GetValue(valueName).ToString();
MessageBox.Show(value32);

}
string value64 = string.Empty;
string value32 = string.Empty;

const string subkey = @"Test";
const string valueName = "Testvalue";

RegistryKey localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
localKey = localKey.OpenSubKey(subkey);
if (localKey != null)
{
value64 = localKey.GetValue(valueName).ToString();
MessageBox.Show(value64);

}
RegistryKey localKey32 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
localKey32 = localKey32.OpenSubKey(subkey);
if (localKey32 != null)
{
value32 = localKey32.GetValue(valueName).ToString();
MessageBox.Show(value32);

}
I'm in a lot of trouble to read a single value from the registry i've tried various codes snippets but everytime my code just doesnt find the key
26 Replies
Remaetanju
Remaetanju6mo ago
I just spent approximately 18h on this problem id like to understand
jcotton42
jcotton426mo ago
the key is at HKLM\Test?
Remaetanju
Remaetanju6mo ago
the full path where my key is look likes HKEY_CURRENT_USER\\System\\GameConfigStore\\Children\\a31f8397-8996-4a7b-8cb9-8cd47c890c54\\ The value i want is named MatchedExeFullPath
jcotton42
jcotton426mo ago
well, that's not at all what the code you've pasted is looking for it's looking for HKLM\Test
Remaetanju
Remaetanju6mo ago
both of them wont be present on a helper's computer
jcotton42
jcotton426mo ago
in any case, what is that key for? might be random per machine since it's a GUID
Remaetanju
Remaetanju6mo ago
its a application path i know its random, but anyway that not THIS specific key that i need but a way to read 1 registry entry but if you want the whole story im trying to get a steam game's root folder from registry
jcotton42
jcotton426mo ago
GitHub
CKAN/Core/Games/KerbalSpaceProgram.cs at master · KSP-CKAN/CKAN
The Comprehensive Kerbal Archive Network. Contribute to KSP-CKAN/CKAN development by creating an account on GitHub.
jcotton42
jcotton426mo ago
seems to look for the Steam install, from there finds the steam game folders from config.vdf, then looks in those for KSP
Remaetanju
Remaetanju6mo ago
ok i've made a bit of a progress im gonna continue coding and show you how it gets thanks ❤️ first problem i see it doesnt detect game if its installed on another disk than steam
jcotton42
jcotton426mo ago
it should at looks at all the BaseInstallFolder values which I'm pretty sure is all your steam libraries
Remaetanju
Remaetanju6mo ago
im looking at it from your link line 35 it get steam install path to steamPath variable then uses KSPDirectory line 43 to combine which i guess to be root install folder of steam + steamapps common and the game name
jcotton42
jcotton426mo ago
you appear to have skipped the entire bit of code that runs if it fails to find it in the default library
Remaetanju
Remaetanju6mo ago
starting at line 50?
jcotton42
jcotton426mo ago
yes
Remaetanju
Remaetanju6mo ago
i got it i should see every BaseInstallFolder when i'm into the if at line 59 ?
jcotton42
jcotton426mo ago
seems like it
Remaetanju
Remaetanju6mo ago
mmmh i may be really dumb i've opened the config.vdf it reads and it doesnt seems to have my libraryfolder (at least in clear text) but i know there is the library.vdf file that might come handy (its been years since i modded ksp, but i had a mere souvenirs of it not find ksp if not on the same disk) y i confirm in my steam/config/config.vds i dont have the path
jcotton42
jcotton426mo ago
are you looking for the library path or the game path?
Remaetanju
Remaetanju6mo ago
the gamepath but i dont understand how ckan reconstruct the game path using config.vdf (for me it seems that all the usefull information it should be using are in config.vdf)
jcotton42
jcotton426mo ago
lemme check on my steam deck actually well that's not what the code is doing it's looking for library paths, then inside those libraries looking for the game
Remaetanju
Remaetanju6mo ago
if (line.Contains("BaseInstallFolder")) this line nevers trigger for me i opened the file line 50
// Attempt to find through config file
string configPath = Path.Combine(steamPath, "config", "config.vdf");
// Attempt to find through config file
string configPath = Path.Combine(steamPath, "config", "config.vdf");
i should be able to find "BaseInstallFolder" when opening the file right ?
jcotton42
jcotton426mo ago
oh hm does seem to be in libraryfolders.vdf idk what's up with ckan then
Remaetanju
Remaetanju6mo ago
i thought i was crazy x) but i think i have everything i need i'll post my code soon 🙂
Mayor McCheese
Mayor McCheese6mo ago
Didn't @🅱ango have some project a while back with steam and install paths and such?
Remaetanju
Remaetanju6mo ago
here's what i've came up with:
private static string[] SteamPaths
=> new string[]
{
Microsoft.Win32.Registry.GetValue(steamRegKey, steamRegValue, null) as string,
};


static string GamePath()
{
// Attempt to get the Steam path.
string? steamPath = null;

foreach (var steam in SteamPaths.Where(p => !string.IsNullOrEmpty(p)))
if (Directory.Exists(steam))
steamPath = steam;

if (steamPath == null)
return null;

// find game folder through library file
string librarypath = Path.Combine(steamPath, "config", "libraryfolders.vdf");

if (File.Exists(librarypath))
{
var stream = File.OpenRead(librarypath);
var kv = KVSerializer.Create(KVSerializationFormat.KeyValues1Text);
KVObject data = kv.Deserialize(stream);

int i = 0;
while (i < data.Count())
{
var libraryfolder = data[i.ToString()]["path"];
var librarygames = data[i.ToString()]["apps"];

foreach (var game in (IEnumerable<KVObject>)librarygames)
if (game.Name.Contains(appID))
return libraryfolder.ToString() + "\\steamapps\\common\\Lethal Company";
i++;
}
}
private static string[] SteamPaths
=> new string[]
{
Microsoft.Win32.Registry.GetValue(steamRegKey, steamRegValue, null) as string,
};


static string GamePath()
{
// Attempt to get the Steam path.
string? steamPath = null;

foreach (var steam in SteamPaths.Where(p => !string.IsNullOrEmpty(p)))
if (Directory.Exists(steam))
steamPath = steam;

if (steamPath == null)
return null;

// find game folder through library file
string librarypath = Path.Combine(steamPath, "config", "libraryfolders.vdf");

if (File.Exists(librarypath))
{
var stream = File.OpenRead(librarypath);
var kv = KVSerializer.Create(KVSerializationFormat.KeyValues1Text);
KVObject data = kv.Deserialize(stream);

int i = 0;
while (i < data.Count())
{
var libraryfolder = data[i.ToString()]["path"];
var librarygames = data[i.ToString()]["apps"];

foreach (var game in (IEnumerable<KVObject>)librarygames)
if (game.Name.Contains(appID))
return libraryfolder.ToString() + "\\steamapps\\common\\Lethal Company";
i++;
}
}
works like a charm thanks again ❤️