i want to get the first name of the logged in user on the current machine,
windows application
i used the following code but i get System.NullReferenceException when it
reaches the Console.Writeline line
string myADSPath = "LDAP://myadelaide.com/CN=Users,DC=myadelaide,DC=com" ;
DirectoryEntry myDirectoryEntry = new DirectoryEntry(myADSPath);
DirectoryEntry mySearchRoot = new DirectoryEntry(myADSPath);
DirectorySearcher myDirectorySearcher =new DirectorySearcher(mySearchRoot);
myDirectorySearcher.Filter="(objectClass=user)";
SearchResult mySearchResult = myDirectorySearcher.FindOne();
myDirectoryEntry.Name
if ( mySearchResult != null )
{
myDirectoryEntry =mySearchResult.GetDirectoryEntry();
Console.WriteLine(myDirectoryEntry.Properties["First
name"].Value.ToString() );
}
though i can get the DirectoryEntry.Name but any other-
DirectoryEntry.Properties["Property name"]- dosen't work though the property
count is 35 when i put DirectoryEntry.Properties into quick watch.
any help please
Paul Clement - 20 Apr 2005 14:11 GMT
¤ i want to get the first name of the logged in user on the current machine,
¤ windows application
¤
¤ i used the following code but i get System.NullReferenceException when it
¤ reaches the Console.Writeline line
¤
¤ string myADSPath = "LDAP://myadelaide.com/CN=Users,DC=myadelaide,DC=com" ;
¤ DirectoryEntry myDirectoryEntry = new DirectoryEntry(myADSPath);
¤ DirectoryEntry mySearchRoot = new DirectoryEntry(myADSPath);
¤ DirectorySearcher myDirectorySearcher =new DirectorySearcher(mySearchRoot);
¤ myDirectorySearcher.Filter="(objectClass=user)";
¤ SearchResult mySearchResult = myDirectorySearcher.FindOne();
¤
¤ myDirectoryEntry.Name
¤ if ( mySearchResult != null )
¤ {
¤ myDirectoryEntry =mySearchResult.GetDirectoryEntry();
¤
¤ Console.WriteLine(myDirectoryEntry.Properties["First
¤ name"].Value.ToString() );
¤
¤
¤ }
¤
¤ though i can get the DirectoryEntry.Name but any other-
¤ DirectoryEntry.Properties["Property name"]- dosen't work though the property
¤ count is 35 when i put DirectoryEntry.Properties into quick watch.
¤
¤ any help please
I think you're looking for givenName:
.GetDirectoryEntry().Properties.Item("givenName").Value
Paul
~~~~
Microsoft MVP (Visual Basic)
Mohamed Farouk - 20 Apr 2005 22:56 GMT
you ar my hero , thanks a lot
> ¤ i want to get the first name of the logged in user on the current machine,
> ¤ windows application
[quoted text clipped - 33 lines]
> ~~~~
> Microsoft MVP (Visual Basic)
Marc Scheuner [MVP ADSI] - 21 Apr 2005 07:01 GMT
>i want to get the first name of the logged in user on the current machine,
>windows application
>any help please
That seems a bit complicated - you could do it a lot easier:
1) Add a reference to the COM "Active DS Type Library" to your project
2) Add a "using ActiveDs; " line to your .cs file
3) Get the current user from the IADsADSystemInfo interface:
IADsADSystemInfo oSysInfo = new ADSystemInfoClass();
string sCurrentUserDN = oSysInfo.UserName;
4) Bind to that user and get it's first name ("givenName")
DirectoryEntry deCurrentUser = new DirectoryEntry("LDAP://" +
sCurrentUserDN);
if(deCurrentUser != null &&
deCurrentUser.Properties.Contains("givenName"))
{
string sFirstName =
deCurrentUser.Properties["givenName"].Value.ToString();
}
HTH
Marc
Mohamed Farouk - 21 Apr 2005 11:12 GMT
thanks marc for your help,
> >i want to get the first name of the logged in user on the current machine,
> >windows application
[quoted text clipped - 24 lines]
> HTH
> Marc