【问题标题】:How do i access the remote desktop services user profile path programatically?如何以编程方式访问远程桌面服务用户配置文件路径?
【发布时间】:2017-12-27 20:27:42
【问题描述】:

因此,其中有一些帖子有点“旧”,但我刚刚遇到这个问题,无法掌握这些属性。

基本上,我需要的是能够访问 Active Directory 中的“远程桌面服务用户配置文件路径”。我已经在网上尝试了几件事,包括用于 powershell 的 ADSI,但是“TerminalServicesHomeDirectory”属性或属性,无论你想调用什么,它都会返回,里面什么都没有。我还尝试了以下应该能够设置它的代码。这也不起作用。

        PrincipalContext PC = new PrincipalContext(ContextType.Domain);
        UserPrincipal UP = UserPrincipal.FindByIdentity(PC, "testact");
        DirectoryEntry DE = (DirectoryEntry)UP.GetUnderlyingObject();
        DE.InvokeSet("TerminalServicesHomeDirectory", new object[] { "testing" });
        DE.CommitChanges();

我知道我在某处读到您应该使用“tsuserex.dll”,但我一直未能成功将其添加为参考。我在我的机器上找到了其中的两个,一个在 system32 中列出,一个在“c:\Windows\WinSxS\amd64.......”中,但它们都没有加载。他们只是产生一个错误

"a reference to $pathhere\tsuserex.dll could not be added. please make sure
the file is accessible, and that it is a valid assembly or COM component."

我已经在互联网上搜索了很多,所以现在我希望全能的 SO 可以来拯救我。顺便说一下,我使用的是 64 位 Windows 10 机器。

【问题讨论】:

  • 没关系,显然这段代码有效。我在尝试 100 次后将其发布到 SO,它随机开始工作而没有任何代码更改。你们吓得投降了:)谢谢。
  • 一些 PS 建议:Thisthis
  • 对于遇到此问题的任何人,您还可以使用 DE.InvokeGet("TerminalServicesProfilePath") 来获取值。
  • 我只是在这里为其他人回答了这个问题:stackoverflow.com/questions/45106182/… 忽略他们在 C# 内部使用解决方案的事实,所有必要的 PowerShell 都存在。
  • Ty,我的解决方案比这简单得多,尽管它只有 4 行短代码。

标签: c# powershell active-directory windows-10 windows-server-2008-r2


【解决方案1】:

原来我的解决方案是正确的,我猜只是花了一点时间来更新。

    PrincipalContext PC = new PrincipalContext(ContextType.Domain);
    UserPrincipal UP = UserPrincipal.FindByIdentity(PC, "testact");
    DirectoryEntry DE = (DirectoryEntry)UP.GetUnderlyingObject();
    //To Change the value
    DE.InvokeSet("TerminalServicesHomeDirectory", new object[] { "testing" });
    DE.CommitChanges();
    //to get the value
    string value = DE.InvokeGet("TerminalServicesHomeDirectory");

这是获取或更改这些值所需的所有代码。您也可以使用 TerminalServicesProfilePath 属性来完成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多