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

10,385

Solution 1

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.

Solution 2

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.

Solution 3

At .Net 5 install the following nuget package System.DirectoryServices

Solution 4

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>

Solution 5

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>
Share:
10,385
OtoNoOto
Author by

OtoNoOto

Updated on June 05, 2022

Comments

  • OtoNoOto
    OtoNoOto almost 2 years

    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?

  • OtoNoOto
    OtoNoOto about 7 years
    Actually I am trying: @using System.DirectoryServices; when I get the error. Screeshot s22.postimg.org/3uwb84egh/…
  • OtoNoOto
    OtoNoOto about 7 years
    Actually I am trying: @using System.DirectoryServices; when I get the error. Screeshot s22.postimg.org/3uwb84egh/…
  • Admin
    Admin about 7 years
    @OtoNoOto So you don't get an error about DirectoryEntry not existing in System, as you wrote in your question? You only get an error about DirectoryServices?
  • OtoNoOto
    OtoNoOto about 7 years
    I actually tried that and still same error! I added the the reference you noted in ~\Views\Web.config. I am starting to wonder if this is something that just will not work in cshtml views? Maybe it has to be in a controller? Seems very odd behavior.
  • OtoNoOto
    OtoNoOto about 7 years
    <system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <pages pageBaseType="System.Web.Mvc.WebViewPage"> <namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Optimization"/> <add namespace="System.Web.Routing" /> <add namespace="ActiveDirectoryQuery" /> <add namespace="System.DirectoryServices" />
  • Reyan Chougle
    Reyan Chougle about 7 years
    @OtoNoOto can you restart your Visual Studio and then check?
  • OtoNoOto
    OtoNoOto about 7 years
    In the end switching my Project Target Framework from '.Net Framework 4.5.2' to '.NET Framework 4.5' and resolved the issue! Upvoted your response as I feel it was still helpful and may resolve someone else's issue. Thanks for your response.
  • Reyan Chougle
    Reyan Chougle about 7 years
    @OtoNoOto. Thanks. Do you want me to include your solution in my post?
  • OtoNoOto
    OtoNoOto about 7 years
    @hvd Thanks for pointing out the mistake. I edited the title and post.
  • Kenmeister
    Kenmeister almost 7 years
    'Copy Local' to "True" on my 'System.DirectoryServices' reference helped me as well, but I needed to rebuild the project before I saw the references resolved in the .cshtml file.
  • vinny
    vinny almost 7 years
    Should also note that my project does not have a reference to System.DirectoryServices, just System.DirectoryServices.AccountManagement. The project also targets 4.6.1.
  • TylerH
    TylerH over 4 years
    I created a new ASP.NET WebForms project today in VS2019, targeting .NET 4.6.1, and ported over some code from an old VS2017 project using DirectoryServices and targeting 4.6.1, and ran into the issue OP had. This answer resolved it whereas none of the others did.
  • WWC
    WWC about 4 years
    For System.DirectoryServices: <add assembly="System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  • KWallace
    KWallace almost 2 years
    This fixed it for me. But why was adding the reference in project references not enough? Why am I having to make that additional manual update to web.config?