Reputation: 1
I want to access some files in the MyDocuments
folder when I run my service on WIndows.
The documentation says that in case of Windows GetDocumentsPath
returns:
C:\Documents and Settings\<username>\My Documents
(XP)C:\Documents and Settings\<username>\My Documents
(Vista or later)In my case I get C:\WINDOWS\system32\config\systemprofile\Documents
. Is it some kind of link to the Documents directory in Windows? Btw, I see no Documents
folder in the C:\WINDOWS\system32\config\systemprofile
path.
Can someone explain this to me?
Upvotes: 0
Views: 264
Reputation: 596256
The Documents folder is a per-user folder. TPath.GetDocumentsPath()
returns the Documents folder of the user account that is associated with the calling thread. But if your service is running under the SYSTEM
account, not a particular user account, you cannot use TPath
to obtain the path of any user-specific folder.
In order for a service to retrieve a user's Documents folder (or any other user-specific folder), the service must either:
be running as the desired user to begin with, not the SYSTEM
account.
if running as the SYSTEM
account, obtain a token for the desired user account, and then pass that token to SHGetFolderPath()
or SHGetKnownFolderPath()
.
WTSEnumerateSessions()
and WTSQuerySessionInformation()
to locate the user's login session, and then use WTSQueryUserToken()
to get that session's user token.LogonUser()
to get the user token, and then load the user's profile into memory with LoadUserProfile()
before querying the folder path.Upvotes: 2