Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting user directory 2

Status
Not open for further replies.

gregaug

Programmer
Sep 9, 2005
61
US
Hello,

I was wondering if there was any way to get a user's directory (e.g. c:\documents and settings\gregaug). I know you can get the temp directory (c:\documents and settings\gregaug\local settings\temp), but I would like to write a user-specific file that isn't as likely to get deleted.

thanks for any help
 
Try executing this line of code
Code:
MsgBox "c:\documents and settings\" & Environ$("username")
and see if it's what you want.

HTH

Bob
 
Can't the user change the environment variables thus making the above code unreliable or am I thinking incorrectly?

Here is another method that does not use environment variables:

faq222-429

Swi
 
Well, the user can change user environment variables. The user can only change system environment variables if logged in as an administrator, and also a system restart. So, it's unlikely in my mind that an admin will modify the "username" variable to another name.

If I were worried about this, I would want to make sure that your API code isn't broken if an admin changes the EV name, too. Probably not, and overall your code is very likely to be more robust.

Bob
 
In fact, I just checked the registry area for system variables and username isn't there. I suspect there's probably a way to modify it, but if so only a very few people know how to.

Bob
 
Bob, your code will still trouble if Windows is not installed on C:\ drive. In that case "Documents and Settings" folder will be present on the same drive as that of Windows.

Its better to use USERPROFILE environment variable which returns the complete path of desired folder.
[tt]
MsgBox Environ$("USERPROFILE")
[/tt]
Alternatively, if environment varaibles are not desired than there are two more options.

1. Use Shell automation.
[tt]MsgBox CreateObject("Shell.Application").NameSpace(40).Self.Path '40 = ssfPROFILE[/tt]

2. Use SHGetSpecialFolderPath API function.
___
[tt]
Option Explicit
Private Declare Function SHGetSpecialFolderPath Lib "shell32" Alias "SHGetSpecialFolderPathA" (ByVal hwnd As Long, ByVal pszPath As String, ByVal csidl As Long, ByVal fCreate As Long) As Long
Const CSIDL_PROFILE = &H28
Private Sub Form_Load()
Dim S As String * 1000
SHGetSpecialFolderPath hwnd, S, CSIDL_PROFILE, 0
MsgBox S
Unload Me
End Sub[/tt]
 
Thanks for the information. A star for both of you.

Swi
 
>...will still trouble if Windows is not installed on C:\ drive. In that case "Documents and Settings" folder will be present on the same drive as that of Windows.

Not only that, but on different language locals the name may not be "Documents and Settings", but the equivalent in that language. And there also may be differences between Windows versions (Vista, for example)
 
BobRodes said:
So, it's unlikely in my mind that an admin will modify the "username" variable to another name.
They don't need to be admin. USERNAME is a user variable

BobRodes said:
In fact, I just checked the registry area for system variables and username isn't there. I suspect there's probably a way to modify it, but if so only a very few people know how to.
it's a user variable

BobRodes said:
I suspect there's probably a way to modify it, but if so only a very few people know how to.
S'easy ...

My Computer/Properties/Advanced/Environment Variables

Then elect to add a new user variable. Call it USERNAME and set it to what you like. This then replaces the system supplied default.

It's that easy. And it is why I prefer to use alternativce methods (where possible) to get this sort of information
 
Stupid of me. I should have thought to try to do what you suggest before making a suggestion. Of course, you can easily create whatever username variable you want.

Ok, so I would toss my suggestion. Strongm, over and above Hypetia's suggestions, do you have further alternatives?

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top