How to get user home directory on Windows

G

Giampaolo Rodola'

Hi all,
I'm trying to use the pywin32 extension to find out the user's home
directory but currently I didn't find a solution yet.
What I'd need to do is not getting the home directory of the currently
logged in user but something like:
"C:\home\users\josh"

Is there a way to do that?
I tried to search through the Pywin32 documentation with no luck.
In addition I'm not practiced with the Windows API at all.
 
G

Giampaolo Rodola'

Update.
I found a way for getting the home directory of the user but it
requires to validate the user by providing username+password:

def get_homedir(username, password):
token = win32security.LogonUser(
username,
None,
password,
win32security.LOGON32_LOGON_NETWORK,
win32security.LOGON32_PROVIDER_DEFAULT
)
return win32profile.GetUserProfileDirectory(token)

What I'd like to do is avoiding the requirement of the password, the
same way as if I would on UNIX where it would be enough just using the
pwd module:
'/home/user'
 
T

thebjorn

Update.
I found a way for getting the home directory of the user but it
requires to validate the user by providing username+password:

def get_homedir(username, password):
token = win32security.LogonUser(
username,
None,
password,
win32security.LOGON32_LOGON_NETWORK,
win32security.LOGON32_PROVIDER_DEFAULT
)
return win32profile.GetUserProfileDirectory(token)

What I'd like to do is avoiding the requirement of the password, the
same way as if I would on UNIX where it would be enough just using the
pwd module:

'/home/user'

Check out http://msdn2.microsoft.com/en-us/library/bb762181(VS.85).aspx
for some of the complexities of special directories on Windows.

If you give more details on what you need to get done, someone might
come up with a better solution (my instinct tells me this might be a
database problem, but then I'm a database person so that might not be
terribly relevant ;-)

-- bjorn
 
T

Tim Golden

thebjorn said:
Check out http://msdn2.microsoft.com/en-us/library/bb762181(VS.85).aspx
for some of the complexities of special directories on Windows.

Part of the problem here is that is the OP is asking for the
"home dir" of a user, but there is no such thing under Windows.
Or, rather, there are several such things. The code he posts
above will get the path to what will be the user's %USERPROFILE%
env var when he logs on. (Which is certainly one of the
definitions of "home dir"). But he wants that path *without* having
to log them in. The page you refer to (and other similar suggestions
of using the os.expanduser function which essentially does the same
thing for you) assume you're already logged in as the user in question.

I've posted a (WMI-based) solution [1] on the python-win32 list
where the OP copied his question. You could do the same just with
win32security and _winreg. (Left as an exercise... etc.) Haven't
yet heard back from the OP as to whether that's given him what he
wants or not.

TJG

[1] http://mail.python.org/pipermail/python-win32/2008-January/006656.html
 
M

Martin P. Hellwig

Giampaolo said:
Hi all,
I'm trying to use the pywin32 extension to find out the user's home
directory but currently I didn't find a solution yet.
What I'd need to do is not getting the home directory of the currently
logged in user but something like:

"C:\home\users\josh"

Is there a way to do that?
I tried to search through the Pywin32 documentation with no luck.
In addition I'm not practiced with the Windows API at all.

Well, windows, to my knowledge, uses the same base path for all profiles
(this is not true for the My Documents folder which can differ). So what
you could do is get the location from the ALLUSERPROFILE environment
variable, go one folder higher and iterate through that.
Ignoring the folders for the 'pseudo' users: 'All Users', 'Default
User', 'LocalService' and 'NetworkService'.

hth
 
T

Tim Golden

Martin said:
Well, windows, to my knowledge, uses the same base path for all profiles
(this is not true for the My Documents folder which can differ). So what
you could do is get the location from the ALLUSERPROFILE environment
variable, go one folder higher and iterate through that.
Ignoring the folders for the 'pseudo' users: 'All Users', 'Default
User', 'LocalService' and 'NetworkService'.

The trouble with that is that any particular user's profile can be
specifically overridden. I'm not attached to an AD network here,
but I think a networked user's profile can be network based
(whike a local user's won't be, say). And there are other ways
to change the profile too, down to hacking the registry as
described here:
http://support.microsoft.com/kb/314843/#XSLTH3129121125120121120120

That said, it'll probably work for a lot of the people for
a lot of the time.

TJG
 
L

Lie

Well, windows, to my knowledge, uses the same base path for all profiles
(this is not true for the My Documents folder which can differ). So what
you could do is get the location from the ALLUSERPROFILE environment
variable, go one folder higher and iterate through that.
Ignoring the folders for the 'pseudo' users: 'All Users', 'Default
User', 'LocalService' and 'NetworkService'.

hth

There is one problem with that, sometimes the folders for the users
are not the same with the user name, usually because of deleting user
name without deleting the profile, then recreate a user with similar
name. It doesn't happens everytime, but it could.

Possibly it is possible to get the SID of the user name (using the way
described in Tim Golden's Microsoft Link' post), then find the user
directory from the registry: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft
\Windows NT\CurrentVersion\ProfileList\[PUT SID HERE]\Profile Image
Path
 
G

Giampaolo Rodola'

Thanks to Tim Golden suggestions I solved my problem.
....In case it would help someone:


<code>
import _winreg
import win32security

username = 'Administrator'
sid = win32security.ConvertSidToStringSid(
win32security.LookupAccountName(None, username)[0]
)
key = _winreg.OpenKey(
_winreg.HKEY_LOCAL_MACHINE,
r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" + "\
\" + sid
)
value, type = _winreg.QueryValueEx(key, "ProfileImagePath")
print value
</code>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top