2011-08-18 157 views

回答

1

// seaching網頁後,我用下面的代碼:

public void myMethod(String p_UserName) 
    { 
     byte[] logonHours = null; 

     logonHours = (byte[])de.Properties["logonHours"].Value; 
     BitArray bitArray = new BitArray(logonHours); 

     for (int i = 0; i < bitArray.Count; i++) 
     { 
      //If bit is zero (that is false), logonHours is Locked 
      //If bit is one (that is true) then set to zero (false) to LOCK the account 
      if (bitArray[i]) 
      { 
       bitArray.Set(i, false); 
      } 
     } 

     byte[] m_Bytes1 = MyExtension.ConvertToByteArray(bitArray); 


     de.Properties["logonHours"].Value = m_Bytes; 
     de.CommitChanges(); 

    } 

靜態類MyExtension {

public static byte[] ConvertToByteArray(this BitArray bits) 
    { 
     int numBytes = bits.Count/8; 
     if (bits.Count % 8 != 0) numBytes++; 

     byte[] bytes = new byte[numBytes]; 
     int byteIndex = 0, bitIndex = 0; 

     for (int i = 0; i < bits.Count; i++) 
     { 
      if (bits[i]) 
       bytes[byteIndex] |= (byte)(1 << (7 - bitIndex)); 

      bitIndex++; 
      if (bitIndex == 8) 
      { 
       bitIndex = 0; 
       byteIndex++; 
      } 
     } 

     return bytes; 
    }//end of method 

}