Finding Desktop Logged in name

Kintara

Member
Licensed User
Longtime User
Is there a simple way to find out the username of whoever is logged in to the desktop at runtime?
I wish to know who has updated entries in a SQL database, and think getting the (corporate) users log in name would be the easiest way to go.

Thanks

Kintara :cool:
 

Basic4Life

Member
Licensed User
Here are 3 ways to get the username.

1. Use the door library with this:
B4X:
AddObject("objEnv","Object")
objEnv.New1(False)
objEnv.CreateNew("System.Environment" & objEnv.System_Mscorlib)
username    = objEnv.GetProperty("UserName")
machinename = objEnv.GetProperty("MachineName")

2. Use the Command line to write the username to a file with this:
B4X:
Shell("cmd"," /Q /C echo %username% > "&AppPath&"\username.txt")
Sleep(500)
If FileExist(AppPath&"\username.txt") = True Then 
   FileOpen(c1, AppPath&"\username.txt", cRead)
   username = FileReadToEnd(c1)
   FileClose(c1)
   FileDel(AppPath&"\username.txt")
End If

3. Use the attached Environment library with this:
B4X:
AddObject("env","Environment")
env.New1
username = env.UserName

If you are already using the Door library, then the first way is probably the easiest.
The command line way, doesn't require an additional library, on very rare occasions the cmd window pops up, despite using the /Q parameter which should surpress that. I'm not sure if it's 100% reliable if Account permissions interfer with the file writing.
The Environment library is just a quick wrapper I wrote to access the System.Environment Class(desktop only), it gives you access to username, machinename and let's you get any other variable via GetSystemVariable(source code included so it gets merged when compiling)
 

Attachments

  • env.zip.zip
    1.7 KB · Views: 269
Last edited:

Kintara

Member
Licensed User
Longtime User
Getting the Desktop Username

Here are 3 ways to get the username.

1. Use the door library with this:
B4X:
AddObject("objEnv","Object")
objEnv.New1(False)
objEnv.CreateNew("System.Environment" & objEnv.System_Mscorlib)
username    = objEnv.GetProperty("UserName")
machinename = objEnv.GetProperty("MachineName")

2. Use the Command line to write the username to a file with this:
B4X:
Shell("cmd"," /Q /C echo %username% > "&AppPath&"\username.txt")
Sleep(500)
If FileExist(AppPath&"\username.txt") = True Then 
   FileOpen(c1, AppPath&"\username.txt", cRead)
   username = FileReadToEnd(c1)
   FileClose(c1)
   FileDel(AppPath&"\username.txt")
End If

3. Use the attached Environment library with this:
B4X:
AddObject("env","Environment")
env.New1
username = env.UserName

If you are already using the Door library, then the first way is probably the easiest.
The command line way, doesn't require an additional library, on very rare occasions the cmd window pops up, despite using the /Q parameter which should surpress that. I'm not sure if it's 100% reliable if Account permissions interfer with the file writing.
The Environment library is just a quick wrapper I wrote to access the System.Environment Class(desktop only), it gives you access to username, machinename and let's you get any other variable via GetSystemVariable(source code included so it gets merged when compiling)

Many thanks.
I was using a simular method to 1) using excel, but it relied on the macro being able to run. Your No2 works a treat but Ill try No3. when i can get on a machine. Excellent suggestions. Thank you
:sign0142:

Kintara :cool:
 

Kintara

Member
Licensed User
Longtime User
Solved!!

I used your enviroment library and it worked a treat!

Thank you

Kintara :cool:
 
Top