在MSDN你可以发现:
在支持LDAP的常用目录,没有一个值的属性不存在。当通过更改,替换或追加操作将属性值设置为非空值时,如果该属性尚不存在,则会创建该属性。同样,如果一个属性被修改为没有值(或值),整个属性被删除。有时您可能想要将属性设置为null。尽管在支持LDAP的目录中不存在此概念,但您可以通过完全删除该属性并指定要清除该属性来完成此操作。
下面是使用System.DirectoryServices
一个例子:
/* Connection to Active Directory
*/
DirectoryEntry deBase = new DirectoryEntry("LDAP://192.168.183.220:389/dc=societe,dc=local", "administrateur", "adm");
/* Directory Search
*/
DirectorySearcher dsLookForOUs = new DirectorySearcher(deBase);
dsLookForOUs.Filter = "(objectCategory=organizationalUnit)";
dsLookForOUs.SearchScope = SearchScope.Subtree;
dsLookForOUs.PropertiesToLoad.Add("cn");
dsLookForOUs.PropertiesToLoad.Add("ou");
dsLookForOUs.PropertiesToLoad.Add("telephoneNumber");
SearchResultCollection srcOUs = dsLookForOUs.FindAll();
foreach (SearchResult srOU in srcOUs)
{
Console.WriteLine("{0}", srOU.Path);
DirectoryEntry de = srOU.GetDirectoryEntry();
if (de.Properties["TelephoneNumber"].Value!= null)
{
// Both solutions are working. Don't forget to commit
//de.Properties["TelephoneNumber"].Clear();
de.Properties["TelephoneNumber"].Value=null;
de.CommitChanges();
}
}
轻微修正:属性是否可以具有任何值(或空值)取决于属性的语法。例如,IA5String被允许为零长度。 –
然后这不被视为'没有价值',而是'空值'。没有值真的是空的。 – JPBlanc