避免在foreach中打印某些行

问题描述:

嗨我想避免在我的foreach循环(在GetSAM中)打印某些行(有效帐户),而不是打印所有内容。避免在foreach中打印某些行

当我尝试通过注销打印有效帐户的行(在valSAM中)这样做时,在有效帐户曾经存在的区域中将是空白的。我知道这是因为foreach遍历数据库中的所有变量。

我怎样才能消除输出之间的差距?

GetSAM:

//Get SAMAccount 
    private static string GetSAM(string ldapAddress, string serviceAccountUserName, string serviceAccountPassword) 
    { 
     string ldapPath = "LDAP://" + ldapAddress; 
     string ldapFilter = "(&(objectclass=user)(objectcategory=person))"; 
     DirectoryEntry directoryEntry = new DirectoryEntry(ldapPath, serviceAccountUserName, serviceAccountPassword); 
     string readOutput; 
     List<string> list = new List<string>(); 
     List<int> invalid = new List<int>(); 
     using (DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry)) 
     { 
      string samAccountName; 
      directorySearcher.Filter = ldapFilter; 
      directorySearcher.SearchScope = SearchScope.Subtree; 
      directorySearcher.PageSize = 1000; 
      using (SearchResultCollection searchResultCollection = directorySearcher.FindAll()) 
      { 
       **foreach (SearchResult result in searchResultCollection) 
       { 
        samAccountName = result.Properties["sAMAccountName"][0].ToString(); 
        if (valSAM(samAccountName, ldapAddress, serviceAccountUserName, serviceAccountPassword)!= true) 
        { 
         invalid.Add('1'); 
        } 
        list.Add(samAccountName); 
       } //end of foreach** 
       // Count all accounts 
       int totalAccounts = list.Count; 
       // Count all invalid accounts 
       int invalidAccounts = invalid.Count; 
       Console.WriteLine("Found " + invalidAccounts + " invalid accounts out of " + totalAccounts + " user accounts.\nQuery in " + ldapAddress + " has finished.\n"); 
       Console.WriteLine("Press [enter] to continue.\n"); 
       readOutput = Console.ReadLine(); 
      }//SearchResultCollection will be disposed here 
     } 
     return readOutput; 
    } 

valSAM:

//Validate SAMAccount 
    private static bool valSAM(string samAccountName, string ldapAddress, string serviceAccountUserName, string serviceAccountPassword) 
    { 
     string ldapPath = "LDAP://" + ldapAddress; 
     DirectoryEntry directoryEntry = new DirectoryEntry(ldapPath, serviceAccountUserName, serviceAccountPassword); 
     StringBuilder builder = new StringBuilder(); 
     bool accountValidation = false; 
     //create instance fo the directory searcher 
     DirectorySearcher desearch = new DirectorySearcher(directoryEntry); 
     //set the search filter 
     desearch.Filter = "(&(sAMAccountName=" + samAccountName + ")(objectcategory=user))"; 
     //find the first instance 
     SearchResult results = desearch.FindOne(); 
     using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, ldapAddress)) 
     { 
      //if users are present in database 
      if (results != null) 
      { 
       //Check if account is activated 
       bool isAccountActived = IsActive(results.GetDirectoryEntry()); 
       //Check if account is expired or locked 
       bool isAccountLocked = IsAccountLockOrExpired(results.GetDirectoryEntry()); 
       accountValidation = ((isAccountActived != true) || (isAccountLocked)); 
       //account is invalid 
       if (accountValidation) 
       { 
        builder.Append("User account " + samAccountName + " is invalid. "); 
        if ((isAccountActived != true) && (isAccountLocked)) 
        { 
         builder.Append("Account is inactive and locked or expired.").Append('\n'); ; 
        } else if (isAccountActived != true) 
        { 
         builder.Append("Account is inactive.").Append('\n'); ; 
        } 
        else if (isAccountLocked) 
        { 
         builder.Append("Account is locked or has expired.").Append('\n'); ; 
        } 
        else 
        { 
         builder.Append("Unknown reason for status. Contact admin for help.").Append('\n'); ; 
        } 
        accountValidation = false; 
       } 
       //account is valid 
       if ((isAccountActived) && (isAccountLocked != true)) 
       { 
       **//builder.Append("User account " + samAccountName + " is valid.").Append('\n'); 
        accountValidation = true; 
       } 
      } 
      else Console.WriteLine("Nothing found."); 
      Console.WriteLine(builder); 
     }//end of using 
     return accountValidation; 
    } 
+0

所以,你要避免打印该帐户是有效的?那么怎么样在'valSAM'的末尾附近加上'Console.WriteLine(builder)'这行'if(!accountValidation){...}'? – Corak 2015-04-01 08:30:45

+0

Btw。似乎你只是想计算GetSAM中的无效/所有帐户。那么你为什么填写名单?为什么不只有两个'int'变量'totalAccounts'和'invalidAccounts',在启动时用'0'初始化它们,然后在每次向列表中添加一个项目时增量('variable ++;')?另外,你非常非常幸运,就像'List invalid = new List (); invalid.Add('1');'偶数作品... – Corak 2015-04-01 08:37:12

+0

作品!谢谢:) – coder123 2015-04-01 08:38:45

你可能想,如果制造商拥有的东西,否则它会打印一个空行只写。也就是说,如果你要处理所有的新生产线的建造者本身改变

Console.WriteLine(builder); 

if (builder.Length > 0) 
{ 
    Console.WriteLine(builder); 
} 

或只是

Console.Write(builder); 

。如果你打算这样做,请使用StringBuilder.AppendLine(),而不是像那样对'\ n'进行硬编码。

+0

谢谢,但这并不是我真正想要的。祝你有美好的一天 :) – coder123 2015-04-01 08:41:38