|
Post by dagnabitboy on Oct 14, 2019 12:01:33 GMT -5
Trying to figure out how to get the current windows user name so I can store program data in the appropriate folder.
example: C:\Users\current-user-name\AppData\Local\myprogram\program-data.txt.
I assume there is a dll call to determine the 'current-user-name'.
Can someone point me in the right direction. An example would be wonderful!
thanks in advance!
|
|
|
Post by Rod on Oct 14, 2019 13:54:02 GMT -5
The Windows system variable %appdata% stores that path. However Liberty can’t use it directly. I am sure there is an API routine to discover it. I think you should use the built in DefaultDir$ instead.
You should not try and dictate where the user stores files, DefaultDir$ will tell you where the user has chosen to keep the program, keeping resource files there or in a sub directory is an easy choice. You could offer the user a file dialog and let them choose a location but that may be hard for them to find later.
DefaultDir$\resource\myresource is easily found programmatically and needs no user input.
|
|
|
Post by tsh73 on Oct 14, 2019 14:50:37 GMT -5
|
|
|
Post by dagnabitboy on Oct 14, 2019 16:43:17 GMT -5
The Windows system variable %appdata% stores that path. However Liberty can’t use it directly. I am sure there is an API routine to discover it. I think you should use the built in DefaultDir$ instead. You should not try and dictate where the user stores files, DefaultDir$ will tell you where the user has chosen to keep the program, keeping resource files there or in a sub directory is an easy choice. You could offer the user a file dialog and let them choose a location but that may be hard for them to find later. DefaultDir$\resource\myresource is easily found programmatically and needs no user input. That's good advice, and it's probably what I'll end up doing. My intention was to emulate what other programs do, and that is to store data in C:\Users\UserName\AppData\Roaming\myconfig.cfg for example. Your method is a lot easier.
I've gotta stop over complicating things!
Thanks!
|
|
|
Post by Brandon Parker on Oct 15, 2019 19:22:23 GMT -5
For completeness sake, you can get the current user's name using this function.
'Create the Generic Size Struct Struct Size, value As long
Print CurrentUserName$() Wait
Function CurrentUserName$() lpBuffer$ = Space$(_MAX_PATH) + chr$(0) Size.value.struct = Len(lpBuffer$)
Open "advapi32.dll" For DLL As #ADVAPI32 CallDLL #ADVAPI32, "GetUserNameA", lpBuffer$ As ptr, _ Size As struct, _ result As long Close #ADVAPI32
If result Then CurrentUserName$ = Trim$(Left$(lpBuffer$, Size.value.struct)) Else CurrentUserName$ = "UNKNOWN USER" End If End Function
{:0)
Brandon Parker
|
|
|
Post by Brandon Parker on Oct 15, 2019 19:35:23 GMT -5
You can actually get the %appdata% folder by pulling it from the Environment Variables that Windows makes available using the following function.
Print GetEnvironmentVariable$("appdata") Wait
Function GetEnvironmentVariable$(label$) buffer$ = Space$(_MAX_PATH)+chr$(0) nSize = len(buffer$) CallDLL #kernel32, "GetEnvironmentVariableA", label$ As ptr, _ 'label of your environment var buffer$ As ptr, _ 'string buffer to hold value nSize As long, _ 'length of buffer result As long 'length of returned data
GetEnvironmentVariable$ = Left$(buffer$, result) End Function
You can also use the GetSpecialFolder function to return the same path.
CSIDL.APPDATA.ROAMING = 26 Print GetSpecialFolder$(CSIDL.APPDATA.ROAMING) Wait
Function GetSpecialFolder$(CSIDL) path$ = Space$(_MAX_PATH) CallDLL #shell32, "SHGetFolderPathA", _NULL As ulong, _ CSIDL As long, _ _NULL As ulong, _ _NULL As ulong, _ path$ As ptr, _ result As long
GetSpecialFolder$ = Trim$(path$) End Function
Either way is acceptable ...
{:0)
Brandon Parker
|
|