程序以LocalSystem角色运行在Windows Service中
在用户已登陆的情况下启动服务,需要取得当前用户的相关信息
现在不知道用户登入Windows的时间该怎么取(这里不用考虑没有用户登陆的情况)在MSDN中查到C++中可以用
IADsUser.LastLogin //The date and time of the last network login.C#要怎么查询这个呢?
在用户已登陆的情况下启动服务,需要取得当前用户的相关信息
现在不知道用户登入Windows的时间该怎么取(这里不用考虑没有用户登陆的情况)在MSDN中查到C++中可以用
IADsUser.LastLogin //The date and time of the last network login.C#要怎么查询这个呢?
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字符串楼主需要根据实际环境修改。
所以要用WinNT问题已解决
使用
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");
测试通过,都是用的反射