c# - Avoid printing certain rows in foreach -


hi avoid printing rows (valid accounts) in foreach loop (in getsam)as opposed printing everything.

when try commenting away line (in valsam) prints valid accounts, blank in area valid account once was. understand because foreach loops through variables in database.

how able remove gaps between output?

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 accounts                  int totalaccounts = list.count;                 // count 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] continue.\n");                 readoutput = console.readline();             }//searchresultcollection 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 directory searcher         directorysearcher desearch = new directorysearcher(directoryentry);         //set search filter         desearch.filter = "(&(samaccountname=" + samaccountname + ")(objectcategory=user))";         //find first instance         searchresult results = desearch.findone();         using (principalcontext pc = new principalcontext(contexttype.domain, ldapaddress))         {             //if users present in database             if (results != null)             {                 //check if account activated                 bool isaccountactived = isactive(results.getdirectoryentry());                 //check if account expired or locked                 bool isaccountlocked = isaccountlockorexpired(results.getdirectoryentry());                 accountvalidation = ((isaccountactived != true) || (isaccountlocked));                 //account invalid                  if (accountvalidation)                 {                     builder.append("user account " + samaccountname + " invalid. ");                     if ((isaccountactived != true) && (isaccountlocked))                     {                         builder.append("account inactive , locked or expired.").append('\n'); ;                     } else if (isaccountactived != true)                     {                         builder.append("account inactive.").append('\n'); ;                     }                     else if (isaccountlocked)                     {                         builder.append("account locked or has expired.").append('\n'); ;                     }                     else                     {                         builder.append("unknown reason status. contact admin help.").append('\n'); ;                     }                     accountvalidation = false;                 }                 //account valid                 if ((isaccountactived) && (isaccountlocked != true))                 {                  **//builder.append("user account " + samaccountname + " valid.").append('\n');                     accountvalidation = true;                 }             }             else console.writeline("nothing found.");             console.writeline(builder);         }//end of using         return accountvalidation;     } 

you want write if builder has otherwise print empty line. namely, change

console.writeline(builder); 

to

if (builder.length > 0) {     console.writeline(builder); } 

or just

console.write(builder); 

if you're going handle of new lines in builder itself. if you're going that, use stringbuilder.appendline() instead of hardcoding '\n' that.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -