Skip to content

Instantly share code, notes, and snippets.

@ShaRose
Created May 15, 2011 08:59
Show Gist options
  • Save ShaRose/972982 to your computer and use it in GitHub Desktop.
Save ShaRose/972982 to your computer and use it in GitHub Desktop.
Steam User ID Path Finder Method
/// <summary>
/// This gets a Dictionary of steam names and the user ID's needed to access, say, brink's settings.
/// </summary>
/// <returns>Dictionary<string,string>, Key is the steam display name, value is the userid path.</returns>
public static Dictionary<string,string> GetSteamProfiles()
{
string steampath;
try
{
steampath = (string) Registry.CurrentUser.OpenSubKey("Software\\Valve\\Steam").GetValue("SteamPath") + "//userdata";
}
catch(Exception e)
{
throw new FileNotFoundException("Unable to locate steam path via registry.", e);
}
if(!Directory.Exists(steampath))
throw new FileNotFoundException("Unable to locate steam path via registry as found location does not exist.");
Dictionary<string,string> LookupTable = new Dictionary<string, string>();
try
{
foreach (string userdir in Directory.GetDirectories(steampath))
{
if(!File.Exists(userdir + "//Config//localconfig.vdf"))
continue;
string quoted = string.Format("\"{0}\"",userdir.Substring(userdir.LastIndexOf('\\') + 1));
using (StreamReader reader = new StreamReader(userdir + "//Config//localconfig.vdf"))
{
try
{
while (!reader.ReadLine().Trim().EndsWith(quoted)) { }
reader.ReadLine();
string line = reader.ReadLine().Trim();
while (!line.EndsWith("}"))
{
if(line.StartsWith("\"Name\"",StringComparison.InvariantCultureIgnoreCase))
{
line = line.Substring(6).Trim();
line = line.Substring(1, line.Length - 2);
LookupTable.Add(line, new DirectoryInfo(steampath + userdir.Substring(userdir.LastIndexOf('\\'))).FullName);
reader.Close();
break;
}
line = reader.ReadLine().Trim();
}
}
catch (NullReferenceException)
{}
}
}
}
catch (Exception e)
{
throw new Exception("Unexpected error retrieving steam user data.",e);
}
return LookupTable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment