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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get the users My Documents Path

Status
Not open for further replies.

WP

Programmer
Nov 30, 1999
463
CH
Hi,

I have an app that write user preferences to a txt file. Security dont allow Program Files access where the app. is.

How can I get the path to the users My Documents to write there?

Many thanks,

Bill Paton


Check out
 
I posted a short piece of code that will solve this in thread222-436160
 
Documented MSDN Article:
========================
In Visual Basic, start a new Standard EXE project. Form1 is created by default.


Add a CommandButton and a TextBox control to Form1.


In the General Declarations Section of the code window for Form1, paste the following code:



Option Explicit

Private Const S_OK = &H0 ' Success
Private Const S_FALSE = &H1 ' The Folder is valid, but does not exist
Private Const E_INVALIDARG = &H80070057 ' Invalid CSIDL Value

Private Const CSIDL_LOCAL_APPDATA = &H1C&
Private Const CSIDL_FLAG_CREATE = &H8000&

Private Const SHGFP_TYPE_CURRENT = 0
Private Const SHGFP_TYPE_DEFAULT = 1
Private Const MAX_PATH = 260

Private Declare Function SHGetFolderPath Lib "shfolder" _
Alias "SHGetFolderPathA" _
(ByVal hwndOwner As Long, ByVal nFolder As Long, _
ByVal hToken As Long, ByVal dwFlags As Long, _
ByVal pszPath As String) As Long

Private Sub Command1_Click()
Dim sPath As String
Dim RetVal As Long

' Fill our string buffer
sPath = String(MAX_PATH, 0)

RetVal = SHGetFolderPath(0, CSIDL_LOCAL_APPDATA Or CSIDL_FLAG_CREATE, 0, SHGFP_TYPE_CURRENT, sPath)

Select Case RetVal
Case S_OK
' We retrieved the folder successfully

' All C strings are null terminated
' So we need to return the string upto the first null character
sPath = Left(sPath, InStr(1, sPath, Chr(0)) - 1)
Text1.Text = sPath
Case S_FALSE
' The CSIDL in nFolder is valid, but the folder does not exist.
' Use CSIDL_FLAG_CREATE to have it created automatically
MsgBox "The folder does not exist"
Case E_INVALIDARG
' nFolder is invalid
MsgBox "An invalid folder ID was specified"

End Select
End Sub

Press the F5 key to run the project.


Click the CommandButton control. The TextBox control is filled with the path to the current user's Application Data folder.


Note that the CSIDL_FLAG_CREATE flag is used. If the folder does not exist, then the SHGetFolderPath function creates it for you, fills the string buffer with the path, and returns S_OK. If you do not use the CSIDL_FLAG_CREATE flag, and the folder does not exist, then the SHGetFolder function returns S_FALSE and nothing is placed in your string buffer. To find the location of other special folders, you need to change the nFolder parameter to another CSIDL value. The constants for these values can be found on the MSDN.
 
I believe that SHFolder.DLL was only introduced with W2K. There is a fuller list of CSIDL values, and a bit of expanation, here:


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top