Determining the user's home directory

LHandy

Well-known member
Joined
May 27, 2005
Messages
50
Programming Experience
5-10
[Resolved]: Determining the user's home directory

I'm writing an application that cleans out the Firefox browser's cache folder (among other temp folders and such).

Under Windows2k/XP the cache folder is located in:
c:\documents and settings\<username>\applicatiion data\... etc.

How can I determine where the user's home directory is? I've tried using the Environ function but not all Windows installations have the HOMEPATH environment variable defined.
 
Last edited:
Sure just after I make a post I think I found a solution. Could someone plug this in and see if this gets their "...\Documents and Settings\<username>\Application Data\" path please? Thanks

VB.NET:
Dim AppDataPath As String
Dim reg As RegistryKey
reg = Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
AppDataPath = reg.GetValue("AppData")
MsgBox(AppDataPath)

Note: For some reason when I post this there are two spaces after "Explorer", please remove those before posting into VB.
 
You do know there's a simpler way, right?

Try this instead:
VB.NET:
Dim FilePath As String = Application.UserAppDataPath

I swear it works..... I'm using it right now in an application of mine (in fact I coppied that right out of it).

Tg
 
Oh geez. *sigh* Thanks man, that's way easier. I'm still making the transition from VB6 to VB.NET. I'm still getting used to all the new, helpful stuff it provides.

Edit: Actually no I want this directory without having to parse it:

c:\documents and settings\<username>\application data\

Application.UserAppDataPath returns that directory, plus the program name and a few following directories. The only way I can seem to do it without hard coding it is the code I've shown above. From that directory I add mozilla\firefox\etc...
 
Last edited:
you can use the system.environment to get lots of "special" folders:
VB.NET:
System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
this get's the logged in user's desktop
 
Then it's UserDataPath ... with out the App part.

Tg
 
TechGnome said:
Then it's UserDataPath ... with out the App part.

Tg

this:
VB.NET:
System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

returns a string path for:
c:\documents and settings\<username>\application data\

and only this which is exactly what you need
 
Back
Top