aardvark
aardvark

Reputation: 1

error CS0234: The type or namespace name 'AccountManagement' does not exist in the namespace 'System.DirectoryServices'

I'm trying to reference System.DirectoryServices.AccountManagement for a project, but keep getting this error. I already went to Project > Add Reference in VS 2017 and included System.DirectoryServices.AccountManagement.dll. After that, I went into the Reference Properties in the Solution Explorer and set the 'Copy Local' attribute to true. Here is the full error message from csc:

Microsoft (R) Visual C# Compiler version 2.8.3.63029 (e9a3a6c0)
Copyright (C) Microsoft Corporation. All rights reserved.

Program.cs(7,32): error CS0234: The type or namespace name 'AccountManagement' does not exist in the namespace 'System.DirectoryServices' (are you missing an assembly reference?)

Here's more info:

using System;
using System.DirectoryServices; 
using System.DirectoryServices.AccountManagement; 
...
PrincipalContext context = new PrincipalContext(ContextType.Domain, "mycompany.local");
GroupPrincipal findAllGroups = new GroupPrincipal(context, "*");
PrincipalSearcher ps = new PrincipalSearcher(findAllGroups); 

Upvotes: 0

Views: 2556

Answers (2)

Igby Largeman
Igby Largeman

Reputation: 16747

You may be targeting an older version of the .Net Framework. Go to Project Properties and look in the Application tab under Target framework. Ensure it's 4 or later.

enter image description here

If that isn't the issue, doing a Clean and Rebuild will sometimes fix this.

Upvotes: 1

Sql Surfer
Sql Surfer

Reputation: 1422

If you want to try something, then try this possibility.

Create Two Dummy Extra Functions. Call the First One in your code somewhere.

After a two pass compile (which will probably still fail). Write some code in the editor to force the "Intellisense" to lookup some enumeration values from the assembly.

Then try to compile again. (Shot in the dark).

Once you get it to compile you probably can delete these two functions.

Here are the functions.

    private void CallThisToForceRefrence()
    {
        int x = 0;
        x = 1;
        x = (x + 1);
        if (x == 42)
        {
            DummyForRoslyn(); //will never execute
        }
        return;
    }

    private void DummyForRoslyn()
    {
        System.DirectoryServices.AccountManagement.PrincipalContext fakeCtx = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain, "FakeNeverGoingToExecute");
        int sillyNum = 0;
        sillyNum = (int)fakeCtx.ContextType;
        if (sillyNum == (int)System.DirectoryServices.AccountManagement.ContextType.Domain)
        {
            // Does this Enum Refrence Force Roslyn to Go Looking in a First Refrence - First Compile Scenario ?
            sillyNum = 42;
        }
        return;
    }

Upvotes: 0

Related Questions