How to read TermainsServices IADsTSUserEx Property from LDAP in C#?

I have read the following properties from AD,


I've tried DirectoryEntry and DirectorySearcher. But they does not include the properties.

I found some example in vbscript and VC to read them. However I failed to make it working in C#. Am I missing some tricky thing?

EDIT: Am I have to run it on "Windows Server" to make it works? Can it be read from win XP?


I don't remember exactly, but it's something like this:

//user is a DirectoryEntry
IADsTSUserEx adsiUser = (IADsTSUserEx)user.NativeObject; 

then you can get the TerminalServices properties you want via adsiUser.

From my experience you're better off developing on a Windows Server with access to AD due to the libraries you use. Then you'll probably make the above work, too :)

I think you can use the InvokeGet method on your DirectoryEntry, passing in the name of the property you want to read.

2008-12-10 11:50 CET — Edited in response to the comment

If I specify a garbage property name, I get the same COM exception. Are you sure the properties you're trying to retrieve are part of the AD schema?

Just to make sure, the code I'm using is as follows:

using (DirectorySearcher searcher = new DirectorySearcher("(cn=Test)"))
    SearchResult result = searcher.FindOne();
    if (result != null)
        DirectoryEntry entry = result.GetDirectoryEntry();
        string s = entry.InvokeGet("TerminalServicesHomeDrive") as string;
        MessageBox.Show(s ?? "null");

This works for me:

            DirectoryEntry user = new DirectoryEntry("LDAP://" + sLDAP_SERVER + "/cn=" + SAMAccount + "," + sLdapFullPath, sUser, sPwd);

            //ActiveDs.IADsUser iADsUser = (ActiveDs.IADsUser)user.NativeObject;
            ActiveDs.IADsUser cont = null;

            cont = user.NativeObject as ActiveDs.IADsUser;

            TSUSEREXLib.IADsTSUserEx m_TsUser = (TSUSEREXLib.IADsTSUserEx)cont;
            int m_TSLogonDisabled = 0;

            m_TsUser.AllowLogon = m_TSLogonDisabled;

Need Your Help

How to set a particular style to the first element in a CollectionView in XAML?

c# xaml windows-8 microsoft-metro

I was seeing Metro style apps, and I realized that the XAML style in the first element of a CollectionView has a different styles from the anothers.

How to print a double with a comma

c++ floating-point double

In C++ I've got a float/double variable.