Windows 平台不支持 System.Directory Services

问题描述 投票:0回答:2

我对软件开发和这个网站都很陌生。 我的问题是关于 Windows 上的 System.DirectoryServices 开发。 我启动了一个普通的 Visual Studio 2019 c# .net 5.0 项目,并添加了 .dll 文件的引用以及它的 nuget 包,甚至是 Windows 兼容性包。 我还将目标框架从.net5.0更改为.net5.0-windows,但它仍然显示“此平台不支持System.DirectoryServices” 每次我尝试创建 DirectoryEntry 时,它都会给我上面提到的消息。

按照我的代码生成用户:

try
            {
                DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
                DirectoryEntry NewUser = AD.Children.Add(du.Name, "user");
                NewUser.Invoke("SetPassword", new object[] { du.password });
                NewUser.Invoke("Put", new object[] { "Description", "Test User from .NET" });
                NewUser.CommitChanges();
                DirectoryEntry grp;

                grp = AD.Children.Find("Administrators", "group");
                if (grp != null) { grp.Invoke("Add", new object[] { NewUser.Path.ToString() }); }
                Console.WriteLine("Account Created Successfully");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

有人可以帮助我吗? 我的目标是简单地在 Windows 系统上创建一个新的本地用户。

提前非常感谢您!

我遵循了这里提到的想法: https://learn.microsoft.com/en-us/answers/questions/853176/systemplatformnotsupportedexception-systemdirector.html

c# .net windows directoryservices
2个回答
2
投票

好吧,显然我的问题就这么简单...... 正如所提到的,我很新.. 此平台不支持“System.DirectoryService”意味着我正在开发的框架版本不支持我导入的.dll。

解决办法: 我安装了 VS2022,使用 6.0 .net 框架启动了一个新项目,并加载了“System.DirectoryServices.AccountManagement”和“System.DirectoryServices.Protocols”的 nuget 包,现在它工作正常。


0
投票

我遇到了同样的问题,并且能够使其与 .NET Core 5.0 上的 System.DirectoryServices.AccountManagement v6.0.0 一起使用。不幸的是,目前我无法升级到支持.NET 6.0及以上版本的VS2022。

© www.soinside.com 2019 - 2024. All rights reserved.