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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.