views:

981

answers:

3

I have read the following properties from AD,

TerminalServicesProfilePath
TerminalServicesHomeDirectory
TerminalServicesHomeDrive

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?

+1  A: 

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 :)

chriscena
I tried also, but I got an System.InvalidCastException : Unable to cast COM object of type 'System.__ComObject' to interface type 'TSUSEREXLib.IADsTSUserEx'.
Dennis Cheung
Are you sure the TS ADSI extension DLL is registered in the COM system via regsvr32 (a tip I picked up from Joe Kaplan on the net somewhere).
chriscena
Thanks, I get it works finally. I have to run regsvr32 before my app start. (And I forgot to restart my nunit yesterday)
Dennis Cheung
A: 
Arnout
I had tried it, and it was failed.System.Runtime.InteropServices.COMException : Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
Dennis Cheung
+1  A: 

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;
Robin
Curios, how does the change of interface handle a failure in this example. is m_TsUser set to null, or is an exception raised?
Greg Domjan