Hi 大家好,我想问一个问题,怎么在程序里获取到用户最后一次登录的时间?
注意是:用户是我们现在电脑的真实的登录用户,而不是比如我们开发网站通过注册写入到数据库中的用户我用的方法是:http://technet.microsoft.com/zh-cn/subscriptions/aa394221 微软的Win32 的API。。
但是,这里面有一个很大很大的问题是,在Win7 下面,获取的时间不准确,但是在其他的操作系统中,是没有问题的。代码可以看这里:http://zhidao.baidu.com/question/493521316.html?quesup2
我现在想用下面2个方法:
1.注册表
2.是活动目录。。不知道大家有没有比较好的方法?最好能给一个现成的代码呢!~。谢谢啦。。

解决方案 »

  1.   

    指的是系统登录呢你可以做一个系统服务来判断系统用户的登录。 不可以这样子的,比如你电脑有 lan1、lan2、lan3 这个三个用户,那么当你用lan1登录的时候,其他2个用户的最后登录时间怎么获取??好烦呀。
      

  2.   

    百度有人给你回答了,不要搞这么麻烦。在命令行使用 net user [username] 可以得到登录日期,想办法捕获这个输出,就行了。最简单。不过要确保net.exe存在
      

  3.   

    很简单,写了一个给你哦:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;namespace ConsoleApplication1
    {
        public static class Net
        {
            [StructLayout(LayoutKind.Sequential)]
            public class NET_DISPLAY_USER
            {
                [MarshalAs(UnmanagedType.LPWStr)]
                public string usri1_name;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string usri1_comment;
                public uint usri1_flags;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string usri1_full_name;
                public uint usri1_user_id;
                public uint usri1_next_index;
            }        [StructLayout(LayoutKind.Sequential)]
            public class USER_INFO_11
            {
                [MarshalAs(UnmanagedType.LPWStr)]
                public string usri11_name;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string usri11_comment;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string usri11_usr_comment;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string usri11_full_name;
                public uint usri11_priv;
                public uint usri11_auth_flags;
                public uint usri11_password_age;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string usri11_home_dir;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string usri11_parms;
                public uint usri11_last_logon;
                public uint usri11_last_logoff;
                public uint usri11_bad_pw_count;
                public uint usri11_num_logons;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string usri11_logon_server;
                public uint usri11_country_code;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string usri11_workstations;
                public uint usri11_max_storage;
                public uint usri11_units_per_week;
                public IntPtr usri11_logon_hours;
                public uint usri11_code_page;
            }        [DllImport("Netapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "NetQueryDisplayInformation")]
            public static extern uint QueryDisplayInformation(string ServerName, uint Level, uint Index, uint EntriesRequested, uint PreferredMaximumLength, out uint ReturnedEntryCount, out IntPtr SortedBuffer);        public static uint QueryDisplayInformation(string ServerName, uint Level, uint Index, uint EntriesRequested, uint PreferredMaximumLength, out uint ReturnedEntryCount, out NET_DISPLAY_USER[] SortedBuffer)
            {
                IntPtr pointer;
                uint value = QueryDisplayInformation(ServerName, Level, Index, EntriesRequested, PreferredMaximumLength, out ReturnedEntryCount, out         pointer);            SortedBuffer = new NET_DISPLAY_USER[ReturnedEntryCount];            for (long index = 0; index != ReturnedEntryCount; ++index)
                {
                    IntPtr pointer_ = new IntPtr(pointer.ToInt64() + Marshal.SizeOf(typeof(NET_DISPLAY_USER)) * index);                SortedBuffer[index] = (NET_DISPLAY_USER)Marshal.PtrToStructure(pointer_, typeof(NET_DISPLAY_USER));
                }            ApiBufferFree(pointer);            return value;
            }        [DllImport("Netapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "NetUserGetInfo")]
            public static extern uint UserGetInfo(string servername, string username, uint level, out IntPtr bufptr);        public static uint UserGetInfo(string servername, string username, uint level, out USER_INFO_11 bufptr)
            {
                IntPtr pointer;
                uint value = UserGetInfo(servername, username, level, out pointer);            bufptr = new USER_INFO_11();            Marshal.PtrToStructure(pointer, bufptr);            ApiBufferFree(pointer);            return value;
            }        [DllImport("Netapi32.dll", EntryPoint = "NetApiBufferFree")]
            public static extern uint ApiBufferFree(IntPtr buffer);
        }    class Program
        {
            static void Main(string[] args)
            {
                Net.USER_INFO_11 info;
                Net.UserGetInfo("127.0.0.1", "Administrator", 11, out info);
                Console.WriteLine(new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(info.usri11_last_logon).AddHours(8)); //加8小时因为我们是8时区
            }
        }
    }
    2012-12-1 13:14:39
    Press any key to continue . . .