Click here to Skip to main content
15,886,799 members
Home / Discussions / C#
   

C#

 
QuestionScrollBars in Listview Pin
topksharma198225-Aug-07 7:50
topksharma198225-Aug-07 7:50 
QuestionConsole Menu Application Pin
invalidsyntax101025-Aug-07 7:14
invalidsyntax101025-Aug-07 7:14 
AnswerRe: Console Menu Application Pin
Lutosław25-Aug-07 7:53
Lutosław25-Aug-07 7:53 
GeneralRe: Console Menu Application Pin
invalidsyntax101025-Aug-07 8:52
invalidsyntax101025-Aug-07 8:52 
GeneralRe: Console Menu Application Pin
Lutosław25-Aug-07 9:16
Lutosław25-Aug-07 9:16 
QuestionXML attribute Pin
ytubis25-Aug-07 5:30
ytubis25-Aug-07 5:30 
QuestionTime elapsed since user logged in Pin
Lutosław25-Aug-07 5:25
Lutosław25-Aug-07 5:25 
AnswerRe: Time elapsed since user logged in Pin
Hessam Jalali25-Aug-07 12:16
Hessam Jalali25-Aug-07 12:16 
it can be done using WMI (System.Management namespace),I don't know but maybe .NET has some classes which give you these info.

with WMI
you can retrieve these data through Win32_LogonSession and Win32_LoggedOnUser and extract start time from them.
with subtracting from DateTime.Now you'll have the duration

it would be somewhat like this

class UserLogonData
{
    const string ALLUSERS = "All";

    readonly string userName;
    readonly DateTime loggedOnTime;
    string logonId;

    public string UserName
    {
        get { return this.userName; }
    }
    public DateTime LoggedOnTime
    {
        get { return this.loggedOnTime; }
    }
    public string LogonID
    {
        get { return this.logonId; }
    }
    public TimeSpan LoggedDuration
    {
        get { return DateTime.Now.Subtract(this.loggedOnTime); }
    }

    private UserLogonData(string userName, string logonId, DateTime loggedOnTime)
    {
        this.userName = userName;
        this.logonId = logonId;
        this.loggedOnTime = loggedOnTime;
    }

    public override string ToString()
    {
        return this.userName;
    }

    public static UserLogonData[] GetUsersLogonTime(string userName)
    {
        List<UserLogonData> dataList = new List<UserLogonData>();

        ManagementObjectSearcher mosUser = new ManagementObjectSearcher(new SelectQuery("Win32_LoggedOnUser"));
        ManagementObjectSearcher mosSession = new ManagementObjectSearcher(new SelectQuery("Win32_LogonSession"));

        ManagementObjectCollection mocUser = mosUser.Get();
        ManagementObjectCollection mocSession = mosSession.Get();

        foreach (ManagementObject moUser in mocUser)
        {
            string userLogonAntecedentData = moUser.Properties["Antecedent"].Value.ToString();
            string userLogonUserName = Regex.Replace(userLogonAntecedentData, @".*Name=""(?'name'.+)"".*", "${name}");
            if (userName!=ALLUSERS && userLogonUserName != userName) continue;
            string userLogonDependentData = moUser.Properties["Dependent"].Value.ToString();
            string userLogonSessionId = Regex.Replace(userLogonDependentData, @".*LogonId=""(?'id'\d+)"".*", "${id}");
            foreach (ManagementObject moSession in mocSession)
            {
                string sessionLogonId = moSession.Properties["LogonId"].Value.ToString();

                if (sessionLogonId == userLogonSessionId)
                {
                    string dateTime = moSession.Properties["StartTime"].Value.ToString();

                    string formattedDateTime = Regex.Replace(dateTime,
                        @"(?'year'\d{4})(?'month'\d{2})(?'day'\d{2})(?'hour'\d{2})(?'min'\d{2})(?'sec'\d{2})\.(?'mil'\d{0,3}).*",
                                    @"${year}/${month}/${day} ${hour}:${min}:${sec}.${mil}");

                    dataList.Add(new UserLogonData(userLogonUserName,userLogonSessionId
                                 ,DateTime.Parse(formattedDateTime)));
                }
            }
        }
        return dataList.ToArray();
    }

    public static UserLogonData[] GetUsersLogonTime()
    {
        return GetUsersLogonTime(ALLUSERS);
    }
}


hope the post would be useful

good luck
GeneralRe: Time elapsed since user logged in Pin
Lutosław25-Aug-07 23:21
Lutosław25-Aug-07 23:21 
GeneralRe: Time elapsed since user logged in Pin
Hessam Jalali26-Aug-07 0:26
Hessam Jalali26-Aug-07 0:26 
GeneralRe: Time elapsed since user logged in Pin
Lutosław26-Aug-07 0:51
Lutosław26-Aug-07 0:51 
GeneralRe: Time elapsed since user logged in Pin
Hessam Jalali28-Aug-07 10:39
Hessam Jalali28-Aug-07 10:39 
QuestionProblem to Loading Data via Another Form. Pin
hdv21225-Aug-07 4:49
hdv21225-Aug-07 4:49 
QuestionTag name Pin
ytubis24-Aug-07 23:44
ytubis24-Aug-07 23:44 
AnswerRe: Tag name Pin
Christian Graus25-Aug-07 0:49
protectorChristian Graus25-Aug-07 0:49 
AnswerRe: Tag name 2 Pin
Luc Pattyn24-Aug-07 23:35
sitebuilderLuc Pattyn24-Aug-07 23:35 
GeneralRe: Tag name 2 Pin
hdv21225-Aug-07 7:56
hdv21225-Aug-07 7:56 
GeneralRe: Tag name 2 Pin
Luc Pattyn25-Aug-07 8:21
sitebuilderLuc Pattyn25-Aug-07 8:21 
GeneralRe: Tag name 2 Pin
Paul Conrad25-Aug-07 9:26
professionalPaul Conrad25-Aug-07 9:26 
AnswerRe: Tag name Pin
Luc Pattyn24-Aug-07 23:33
sitebuilderLuc Pattyn24-Aug-07 23:33 
GeneralRe: Tag name Pin
ytubis24-Aug-07 23:45
ytubis24-Aug-07 23:45 
Questionwebhosting Pin
kalyan_241624-Aug-07 22:53
kalyan_241624-Aug-07 22:53 
Answerummk Pin
invalidsyntax101025-Aug-07 7:28
invalidsyntax101025-Aug-07 7:28 
QuestionHow to catch Url from internet explorer in my Downloader? Pin
Pinkesh Gandhi24-Aug-07 22:50
Pinkesh Gandhi24-Aug-07 22:50 
QuestionReusing a socket Pin
Chals24-Aug-07 22:30
Chals24-Aug-07 22:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.