Windows logon/logout times

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aamir47p
    New Member
    • Sep 2008
    • 3

    Windows logon/logout times

    Please help meI want to get windows logon and logoff time and store it in a text file by using vb.net.
    windows logon date/time and windows logout date/time and store it in text file "c:/a.txt
    How i can do it?
    please reply me as soon
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Well, I found this WMI code to get a login time:
    Code:
    ManagementScope ms = new ManagementScope("\\root\\cimv2");
    ObjectQuery oq = new ObjectQuery("Select * from Win32_Session");
    ManagementObjectSearcher query = new ManagementObjectSearcher(ms, oq);
    ManagementObjectCollection queryCollection = query.Get();
    
    foreach (ManagementObject mo in queryCollection)
    {
      if (mo["LogonType"].ToString().Equals("2"))           //  2 - for logged on User
      {
        MessageBox.Show("Started Time: " + mo["StartTime"].ToString());
      }
    }
    Maybe this will help.

    Comment

    • Aamir47p
      New Member
      • Sep 2008
      • 3

      #3
      dear its C# code of windows logon/logout time that you send me.
      I want Vb.net code of windows logon/logout time .

      Comment

      • joedeene
        Contributor
        • Jul 2008
        • 579

        #4
        Originally posted by Aamir47p
        dear its C# code of windows logon/logout time that you send me.
        I want Vb.net code of windows logon/logout time .
        you could try googling or doing some work, and showing some work on your end instead of being picky of help given. if you would have read the POSTING GUIDELINES you would have seen that we dont do EVERYTHING or whole programs for you. but heres a simple translation...

        Originally posted by insertAlias
        ManagementScope ms = new ManagementScope ("\\root\\cimv2 ");
        ObjectQuery oq = new ObjectQuery("Se lect * from Win32_Session") ;
        ManagementObjec tSearcher query = new ManagementObjec tSearcher(ms, oq);
        ManagementObjec tCollection queryCollection = query.Get();

        foreach (ManagementObje ct mo in queryCollection )
        {
        if (mo["LogonType"].ToString().Equ als("2")) // 2 - for logged on User
        {
        MessageBox.Show ("Started Time: " + mo("StartTime") .ToString());
        }
        }
        Dim ms As ManagementScope = New ManagementScope ("\\root\\cimv2 ")
        Dim oq As ObjectQuery = New ObjectQuery("Se lect * from Win32_Session")
        Dim query As ManagementObjec tSearcher = New ManagementObjec tSearcher(ms, oq)
        Dim queryCollection As ManagementObjec tCollection = query.Get()

        For Each mo As ManagementObjec t In queryCollection

        if (mo("LogonType" ).ToString().Eq uals("2"))

        MessageBox.Show ("Started Time: " + mo("StartTime") .ToString())
        End If
        Next

        something like that maybe, idk not tested...

        joedeene

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Well, to be honest, I've not actually used the code. I just did a quick google search to help out.

          Comment

          • PRR
            Recognized Expert Contributor
            • Dec 2007
            • 750

            #6
            Originally posted by Aamir47p
            Please help meI want to get windows logon and logoff time and store it in a text file by using vb.net.
            windows logon date/time and windows logout date/time and store it in text file "c:/a.txt
            How i can do it?
            please reply me as soon

            Code:
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT LastBootUpTime FROM Win32_OperatingSystem"))
                            {
                                foreach (ManagementObject queryObj in searcher.Get())
                                {
                                    strRet = (Convert.ToString(queryObj["LastBootUpTime"]).Trim());
                                }
                            }
            you can get the login time .. but for logoff time you need to poll ... or you can create a service that logs time onshutdown...bu t if a computer is forced restart then you cant know the time.. therefore do some pollin

            "dear its C# code of windows logon/logout time that you send me.
            I want Vb.net code of windows logon/logout time ."

            we are here to help u.. if u have a specific problem.. or need some guidance... remember this is a technical forum....

            How To Ask Questions The Smart Way

            Comment

            Working...