How to get Windows user name?

M

Mike Johnson

Hi, all

I am trying to detect the user who's currently login the Windows system.
Is there way to do it in Ruby?

Thanks
 
S

Siep Korteling

Mike said:
Hi, all

I am trying to detect the user who's currently login the Windows system.
Is there way to do it in Ruby?

Thanks

require 'win32ole'
network=WIN32OLE.new("Wscript.Network")
puts network.username

#or
puts ENV['username']
#for win95/98 use
puts ENV['userid']

#or (stolen from www.,rubytips.org)
require 'Win32API'
name = " " * 128
size = "128"
Win32API.new('advapi32','GetUserName',['P','P'],'I').call(name,size)
puts name.unpack("A*")

Take your pick.

Regards,

Siep
 
L

Luis Lavena

Hi, all

I am trying to detect the user who's currently login the Windows system.
Is there way to do it in Ruby?

Thanks

NT/2K/2K3/Vista systems have a environment variable for interactive
users:

irb(main):001:0> ENV['USERNAME']
=> "Luis"

So you only need to asign ENV['USERNAME'] and will get the current
username.

If your process is not an interactive one (ala, a Windows service)
then this variable will be nil or the special account used for it
(LocalService).

HTH,
 
D

Daniel Berger

Hi, all

I am trying to detect the user who's currently login the Windows system.
Is there way to do it in Ruby?

This is the one function of the 'etc' library that works on Windows:

require 'etc'
puts Etc.getlogin

Or, you can use the sys-admin library, which provides much more
functionality on Windows:

gem install sys-admin
require 'sys/admin'

puts Sys::Admin.get_login

Regards,

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top