程序以LocalSystem角色运行在Windows Service中
在用户已登陆的情况下启动服务,需要取得当前用户的相关信息
现在不知道用户登入Windows的时间该怎么取(这里不用考虑没有用户登陆的情况)在MSDN中查到C++中可以用
IADsUser.LastLogin  //The date and time of the last network login.C#要怎么查询这个呢?

解决方案 »

  1.   

    msdn里有例程,稍加修改,调试通过:
    using System.Reflection;
    using System.DirectoryServices;            DirectoryEntry ent = new DirectoryEntry("LDAP://CN=Xiaojun Li,OU=IT,OU=MyRoad,OU=MyCity,OU=MyCompany,DC=ocnc,DC=cn");
                Object ads = ent.NativeObject;
                Type type = ads.GetType();
                string date = type.InvokeMember("LastLogin",
                                                BindingFlags.GetProperty,
                                                null,
                                                ads,
                                                null).ToString();
    参考:
    MSDN这里的LDAP字符串楼主需要根据实际环境修改。
      

  2.   

    灰常感谢LS的,由于服务运行于客户端
    所以要用WinNT问题已解决
      

  3.   

    http://zhoufoxcn.blog.51cto.com/792419/169000
      

  4.   

    虽然已经结贴,但还要补充一下,有个更简单的写法:DirectoryEntry ent = new DirectoryEntry("LDAP://CN=Xiaojun Li,OU=IT,OU=MyRoad,OU=MyCity,OU=MyCompany,DC=ocnc,DC=cn");string date = ent.InvokeGet("LastDate").ToString();
      

  5.   

    当前机器WIN7(x64)上测试
    使用
    string path = string.Format("LDAP://{0}/{1}", SystemInfo.Work_Station, UserName);
    会报指定的机器不可操作
    MSDN上说域路径使用LDAP本地机器要使用如下的path(我的服务是运行在不同的客户机器上)
    string path = string.Format("WinNT://{0}/{1}", MachineName, UserName);
    string date = ent.InvokeGet("LastLogin");
    测试通过,都是用的反射