简体   繁体   中英

'DirectoryServices' does not exist in the namespace 'System'

I am trying to use DirectoryEntry in a ASP.Net MVC (C#) project and receive the following error:

The type or namespace ' DirectoryServices ' does not exist in the namespace ' System '.

I have added the following references to my project:

System
System.DirectoryServices

The System.DirectoryServices is loading:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.DirectoryServices.dll

Project Target Framework: .Net Framework 4.5.2

This seems like it would typically be a simple reference issue, but as stated above the reference should be there. Any suggestion?

Actually the right way to do this is by adding a reference to DirectoryServices. It has nothing to do with the .NET Framework version.

Right-click on your Project in Solution Explorer and select Add -> Reference, and then select System.DirectoryServices.

It seems you are trying

System.DirectoryEntry directoryEntry = ...

Instead simply use

using System.DirectoryServices;

and try

DirectoryEntry directoryEntry = ...

or you can also try

System.DirectoryServices.DirectoryEntry directoryEntry = ...

Update:

You are trying to use DirectoryEntry in cshtml page. So you would need to add reference of System.DirectoryServices in web.config file under Views folder of your project (it is not the main web.config file in project's root)

<system.web.webPages.razor>
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      <add namespace="System.Web.Mvc" />
      <add namespace="System.Web.Mvc.Ajax" />
      <add namespace="System.DirectoryServices" />
      ....
      <!-- etc -->
    </namespaces>
  </pages>
</system.web.webPages.razor>

In your Views web.config, in addition to adding the namespace, you also have to explicitly add the assembly. Just being added in the your project references doesn't seem to be enough. Here's a views web.config that works for me:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation>
      <assemblies>
        <add assembly="System.DirectoryServices.AccountManagement, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
  <system.web.webPages.razor>
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.DirectoryServices.AccountManagement" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
</configuration>

在 .Net 5 安装以下 nuget 包System.DirectoryServices

It looks like you are trying to load DirectoryEntry from the System namespace, as if you have written this in your code:

System.DirectoryEntry entry = ...

instead of:

System.DirectoryServices.DirectoryEntry e = ...;

or simply:

DirectoryEntry e = ...;

After reading some other posts I tried switching my Project Target Framework from '.Net Framework 4.5.2' to '.NET Framework 4.5' and resolved the issue! It appears System.DirectoryServices has been removed in .Net Framework 4.5.2 which seems odd. Perhaps it has been replaced with something else?

EDIT: Changing the Target Framework resolved issue, but on another WS I was also able to set 'Copy Local' to "True" on my 'System.DirectoryServices' reference and that resolved the issue. So maybe it's an issue with that dll not being in GAC? Anyhow, hope one of these solutions helps someone if they run into the same issue.

It Happens where .cs is under App_code

using <add assembly="System.DirectoryServices.AccountManagement may solve that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM