0

Hi,

I would like to ask if i intend to copy or capture the windows login name, how am i to go about doing it?

I tried

string Name = Environment.GetEnvironmentVariable("USERNAME");
string FirstPresentTime = DateTime.Now.ToString();

then write it to txt file but the name is blank but the Date&time appeared and if i change it to

string Name = ("USER");

the USER and Date&time will both appear.

Thanks,
keat

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by sknake
0
public void WatchLogonEvent(object stateObject)
{
AutoResetEvent stopWatcher = stateObject as AutoResetEvent;
WqlEventQuery q = new WqlEventQuery();
q.EventClassName = "__InstanceCreationEvent";
q.WithinInterval = new TimeSpan(0, 0, 3);
q.Condition = @"TargetInstance ISA 'Win32_NtLogEvent' and
TargetInstance.LogFile='Security' and TargetInstance.EventIdentifier=528";
try
{
using(ManagementEventWatcher watcher = new
ManagementEventWatcher(q))
{
watcher.EventArrived += new
EventArrivedEventHandler(LogonEventArrived);
watcher.Start();
// wait for a stop event
stopWatcher.WaitOne();
watcher.Stop();
}
}
catch(Exception e)
{
// log the event and stop the thread in case of failure, ...
here dump the exception to the debugger
Debug.WriteLine(e);
}
}

or

System.Security.Principal.WindowsIdentity.GetCurrent();

Borrowed from:
http://www.eggheadcafe.com/community/aspnet/2/10041847/current-logged-in-user.aspx

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.