| djberg96 ( @ 2009-07-22 20:41:00 |
| Current mood: | |
| Current music: | Voices of Autumn 2006 (Beautiful) |
How to get a (local) user's home directory on Windows
While getting the current user's home directory is easy enough for the current user if you're willing to rely on ENV['USERPROFILE'], obtaining the home directory of an arbitrary local user isn't so straightforward. Here's the Ruby solution:
require 'win32/security' require 'win32/registry' key = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\" usr = 'some_user' sec = Win32::Security::SID.open(usr) key += sec.to_s Win32::Registry::HKEY_LOCAL_MACHINE.open(key) do |reg| puts reg['ProfileImagePath'] end
While win32-registry is part of the standard library, win32-security will have to be installed.
gem install win32-security
In other news, I'm inlining CSS to get purrtier code. I'm using "style='background: lightgray; padding: 1em; border: 1px solid black; color: black'" if anyone cares.