[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to get Windows user name?

Mike Johnson

8/14/2008 10:50:00 PM

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
--
Posted via http://www.ruby-....

4 Answers

Siep Korteling

8/14/2008 11:13:00 PM

0

Mike Johnson wrote:
> 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
--
Posted via http://www.ruby-....

Luis Lavena

8/14/2008 11:48:00 PM

0

On Aug 15, 12:50 am, Mike Johnson <freezingsm...@gmail.com> wrote:
> 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,
--
Luis Lavena

Daniel Berger

8/15/2008 4:06:00 PM

0

On Aug 14, 4:50=A0pm, Mike Johnson <freezingsm...@gmail.com> wrote:
> 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

Mike Johnson

8/15/2008 4:45:00 PM

0

Thanks all for the help. It's working now. Thanks!

--
Posted via http://www.ruby-....