djberg96 ([info]djberg96) wrote,
@ 2009-07-22 20:41:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Current mood: calm
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.



(4 comments) - (Post a new comment)


[info]drbrain
2009-07-23 10:02 pm UTC (link)
You should set the font color too, my default font color is also very light making it unreadable.

(Reply to this) (Thread)


[info]djberg96
2009-07-23 10:29 pm UTC (link)
I didn't think about that. I've explicitly set it to black, thanks.

(Reply to this) (Parent)


[info]hitchhiker
2009-07-24 12:33 pm UTC (link)
is it worth adding that as a method to one of the win32:: libs?

(Reply to this) (Thread)


[info]djberg96
2009-07-24 12:38 pm UTC (link)
It's now part of the sys-admin library as the 'dir' member, same as the Unix version:
require 'sys/admin'
include Sys

Admin.get_user('djberge').dir # => C:/Users/djberge (Vista)

(Reply to this) (Parent)


(4 comments) - (Post a new comment)

Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…