C# 如何获取本地windows帐户的过期日期?

C# 如何获取本地windows帐户的过期日期?,c#,asp.net,C#,Asp.net,我需要创建如下C#方法: void deleteExpiredAccounts(String Account,dateTime expirationDate) { if(expirationDate == DateTime.Now) { DirectoryEntry localDirectory = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");

我需要创建如下C#方法:

    void deleteExpiredAccounts(String Account,dateTime expirationDate)
    {
      if(expirationDate == DateTime.Now)
      {
        DirectoryEntry localDirectory = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"); 
        DirectoryEntries users = localDirectory.Children;
        DirectoryEntry user = users.Find(Account); 
        users.Remove(user);
      }
    }
如果您有任何想法,我们将不胜感激。提前谢谢

结帐

试一试


除非我大错特错,否则本地帐户不会过期,因此没有此类属性。当您需要在给定时间段后终止帐户的关系时,帐户过期对于域级帐户是有价值的


现在,如果您谈论的是本地帐户密码过期,那完全是另一回事,但我无法想象您为什么要根据过期的密码删除本地计算机用户。

以下代码为我提供了域和本地用户帐户的密码过期日期:

public static DateTime GetPasswordExpirationDate(string userId, string domainOrMachineName)
{
    using (var userEntry = new DirectoryEntry("WinNT://" + domainOrMachineName + '/' + userId + ",user"))
    {
        return (DateTime)userEntry.InvokeGet("PasswordExpirationDate");
    }
}

:S我尝试过,但结果是:“System.DirectoryServices.PropertyValueCollection”@Ragaei try
user.Properties[“maxPwdAge”].Value.ToString()
foreach(user.Properties[“maxPwdAge”]){Console.WriteLine(I)}
@Bala…谢谢你:这就是我正在搜索的:StringBuilder sb=new StringBuilder(); foreach(user.Properties[“AccountExpirationDate”]中的对象i){sb.Append(i);}String ss=sb.ToString();
public static DateTime GetPasswordExpirationDate(string userId, string domainOrMachineName)
{
    using (var userEntry = new DirectoryEntry("WinNT://" + domainOrMachineName + '/' + userId + ",user"))
    {
        return (DateTime)userEntry.InvokeGet("PasswordExpirationDate");
    }
}