C# 命名空间中不存在类型或命名空间名称“DirectoryServices”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10691001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The type or namespace name 'DirectoryServices' does not exist in the namespace?
提问by Ali
CS0234: The type or namespace name 'DirectoryServices' does not exist in the namespace 'System' (are you missing an assembly reference?)
CS0234:命名空间“System”中不存在类型或命名空间名称“DirectoryServices”(您是否缺少程序集引用?)
This page was working fine,show records from directly services with no error. but now it gives the above error.
此页面工作正常,显示直接服务的记录,没有错误。但现在它给出了上述错误。
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="false" DataSourceID="odsUsers"
AllowPaging="true" AllowSorting="true" Width="100%">
<Columns>
<asp:TemplateField HeaderText="User Name">
<ItemTemplate>
<%#((System.DirectoryServices.DirectoryEntry)Container.DataItem).Properties["userPrincipalName"].Value%>
</ItemTemplate>
</asp:TemplateField>
/Columns>
</asp:GridView>
Project builds successfully but when I open the page then it gives error
项目构建成功但是当我打开页面时它会出错
回答by Nate
Looks like you need to add a reference (in your project) to System.DirectoryServices. Since you're using it in what looks like an aspx markup page, sometimes the compiler will let those fly during "build" but fail when you actually execute the page.
看起来您需要将引用(在您的项目中)添加到System.DirectoryServices. 由于您在看起来像 aspx 标记页面的情况下使用它,有时编译器会让它们在“构建”期间运行,但在您实际执行页面时会失败。
回答by Emmie Lewis-Briggman
You need to add an import directive on your asp.net page. Make sure it is fully qualified. Make sure you have a reference to the assembly in your project as well.
您需要在 asp.net 页面上添加导入指令。确保它是完全合格的。确保您也引用了项目中的程序集。
<%@ Assembly Name="System.DirectoryServices, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %>
回答by Kevin
After you add your directory services reference, right click on the referenceand go to properties. Set "CopyLocal"to true.
添加目录服务引用后,右键单击reference并转到properties。设置"CopyLocal"为真。
回答by Eldritch
I had the same problem. I did a search for DirectoryServices.dll in my windows folder. Since all the versions that came up had the same size, I picked one and copied it to the bin folder in my website. If you don't have a bin folder, just create it. Once I did that, I was able to open my webpage without errors.
我有同样的问题。我在 Windows 文件夹中搜索了 DirectoryServices.dll。由于出现的所有版本都具有相同的大小,我选择了一个并将其复制到我网站的 bin 文件夹中。如果您没有 bin 文件夹,只需创建它。一旦我这样做了,我就可以毫无错误地打开我的网页。
回答by Slogmeister Extraordinaire
I'm pretty sure that I'm "publishing" my application from my development box to my IIS box completely incorrectly. However, I found this solution here, and it worked for me.
我很确定我完全错误地将我的应用程序从我的开发箱“发布”到我的 IIS 箱。但是,我在这里找到了这个解决方案,它对我有用。
If you are using web application then in your web.config add the following code.
如果您使用的是 Web 应用程序,则在您的 web.config 中添加以下代码。
<compilation debug="true" targetFramework="4.0" >
<assemblies>
<add assembly="System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.DirectoryServices.AccountManagement, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
回答by DVK
I ran into this issue in Visual Studio 2015 with an MVC project that was targeted for .NET Framework 4.5.2. Changing the target framework to .NET 4.5 resolved the problem.
我在 Visual Studio 2015 中遇到了这个问题,其中有一个针对 .NET Framework 4.5.2 的 MVC 项目。将目标框架更改为 .NET 4.5 解决了该问题。
回答by ShamPooSham
The easy way that worked for me was to right-click on References => Add Reference, and select System.DirectoryServices (and needed subassemblies).
对我有用的简单方法是右键单击 References => Add Reference,然后选择 System.DirectoryServices(和所需的子组件)。
回答by Faiyaz
It will work if assembly "System.DirectoryServices.AccountManagement" is added in the references. Adding System.DirectoryServices will not work.
如果在引用中添加程序集“System.DirectoryServices.AccountManagement”,它将起作用。添加 System.DirectoryServices 将不起作用。
回答by Ali Reza
1 - Right-Click Mouse Button on "References"
2 - Click "Add Reference ..."
3 - Click "Browse" Button
4 - Find the following files in folder:
1 - 在“参考”上单击鼠标右键
2 - 单击“添加参考...”
3 - 单击“浏览”按钮
4 - 在文件夹中找到以下文件:
"C:\Program Files\Reference\Assemblies\Microsoft\Framework\.NETFramework\v4.6.1"
-System.DirectoryServices.dll
-System.DirectoryServices.AccountManagement.dll
“C:\Program Files\Reference\Assemblies\Microsoft\Framework\.NETFramework\v4.6.1”
-System.DirectoryServices.dll
-System.DirectoryServices.AccountManagement.dll
5 - Select them
6 - Press "OK" Button
5 - 选择它们
6 - 按“确定”按钮

