The type or namespace name 'DirectoryServices' does not exist in the namespace?

66,412

Solution 1

After you add your directory services reference, right click on the reference and go to properties. Set "CopyLocal" to true.

Solution 2

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.

If you are using web application then in your web.config add the following code.

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

Solution 3

1 - Right-Click Mouse Button on "References"
2 - Click "Add Reference ..."
3 - Click "Browse" Button
4 - Find the following files in folder:

"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

Solution 4

The easy way that worked for me was to right-click on References => Add Reference, and select System.DirectoryServices (and needed subassemblies).

Solution 5

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.

Share:
66,412
Ali
Author by

Ali

3 years development, 2 years QA,

Updated on July 05, 2022

Comments

  • Ali
    Ali almost 2 years

    CS0234: The type or namespace name 'DirectoryServices' does not exist in the namespace 'System' (are you missing an assembly reference?)

    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

  • Ali
    Ali about 12 years
    System.DirectoryServices assembly is already added,I removed this and added again but same error
  • Ed_
    Ed_ about 9 years
    If you happen to not see your folder 'references' search on the top of the "solution explorer" panel for a 'show all files' option.
  • Arik
    Arik over 5 years
    Nowadays you can just pick from the list, instead of browsing.
  • rpm192
    rpm192 over 3 years
    2020 this still works, although the directory has changed to: C:\Program Files\Reference Assemblies\Microsoft\Framework\*version*