I have this code to check the system username. it works, the msgbox shows the correct username. But in my if statement, the check if UserName = “michael” does not work. Even the Test-MsgBox does not open. UserName is a property.
Whats my fault?
[code] dim sh as new Shell
if TargetWin32 then
sh.Execute(“echo %username%”)
ElseIf TargetMacOS then
sh.Execute(“whoami”)
end If
UserName = sh.Result
if UserName = “michael” then
MsgBox(“TEST”)
FilePreferences.Enabled = False
End If
MsgBox("Hallo " + UserName)[/code]
Regards,
Michael
The obvious question: when you debug this code, what is the value of ‘username’ at the conditional?
[quote=140184:@Michael Bzdega]I have this code to check the system username. it works, the msgbox shows the correct username. But in my if statement, the check if UserName = “michael” does not work. Even the Test-MsgBox does not open. UserName is a property.
Whats my fault?
[code] dim sh as new Shell
if TargetWin32 then
sh.Execute(“echo %username%”)
ElseIf TargetMacOS then
sh.Execute(“whoami”)
end If
UserName = sh.Result
if UserName = “michael” then
MsgBox(“TEST”)
FilePreferences.Enabled = False
End If
MsgBox("Hallo " + UserName)[/code]
Regards,
Michael[/quote]
Have you tried whoami
or echo %username%
at the command prompt ?
Hi Michel,
well, the the msgbox at the end of this code show the correct username. so the app gets the username from the system.
sh.result has a trailing character, so username did not contain Michael, but Michael plus something else invisible.
This works :
UserName = Trim(sh.Result)
Thanks Michel, that works. The Test-MessageBox shows up. But for some reason, i am not able to disable the menuitem FilePreferences in the Applicationmenu. It is still accessible for the user.
[code] dim sh as new Shell
if TargetWin32 then
sh.Execute(“echo %username%”)
ElseIf TargetMacOS then
sh.Execute(“whoami”)
end If
UserName = Trim(sh.Result)
if UserName = “michael” then
MsgBox(“TEST”)
FilePreferences.Enabled = False
End If
MsgBox("Hallo " + UserName)[/code]
ok, i now use FilePreferences.AutoEnable = False instead of FilePreferences.Enabled = False. Now it Works.
[code] dim sh as new Shell
if TargetWin32 then
sh.Execute(“echo %username%”)
ElseIf TargetMacOS then
sh.Execute(“whoami”)
end If
UserName = Trim(sh.Result)
if UserName = “michael” then
MsgBox(“TEST”)
FilePreferences.AutoEnable = False
End If
MsgBox("Hallo " + UserName)[/code]
Thanks Michel and Roger for your help.
Michael
FilePreferences.AutoEnable = False
FilePreferences.Enabled = False
You mean, both are necessary?
ok, i use both in my code now.
Thanks.
On OS X you can use the API to get the username.
Declare Function NSUserName Lib "Foundation" () As CFStringRef
Msgbox NSUserName
No shell required.
Hi Sam,
good to know, but i want to stay cross-plattform.
Thanks.
Sure;
#if targetMacOS then
Declare Function NSUserName Lib "Foundation" () As CFStringRef
Msgbox NSUserName
#else
~
#endif
If there’s a current API for doing it, use the API!