seanrco
seanrco

Reputation: 1069

'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?

Upvotes: 2

Views: 14394

Answers (7)

Mohammad Dayyan
Mohammad Dayyan

Reputation: 22409

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

Upvotes: 2

Avi Avital
Avi Avital

Reputation: 1

It Happens where .cs is under App_code

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

Upvotes: 0

Mert Sevinc
Mert Sevinc

Reputation: 1027

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.

Upvotes: 1

vinny
vinny

Reputation: 1810

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>

Upvotes: 2

seanrco
seanrco

Reputation: 1069

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.

Upvotes: 0

Reyan Chougle
Reyan Chougle

Reputation: 5311

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>

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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 = ...;

Upvotes: 0

Related Questions