Crashing with C# and Directory Services on XP


I'm trying to do some simple data retrieval with C# and Directory Services, but for some reason it doesn't work on any XP machines. If I run my code on a Server 2003 machine, there are no problems. I've spent a fair bit of time trying to find out if maybe there's some redistributable I need on XP or if the functionality simply isn't there, but I've found references to other developers who have similar code working under XP. If anyone has any experience or advice to share, I'd appreciate it.

A simple code snippet that's crashing for me:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.DirectoryServices;

namespace IIS_Site_Query_Tool
    public partial class Window1 : Window
        public Window1()

            DirectoryEntry W3SVC = new DirectoryEntry("IIS://localhost/w3svc");
            foreach (DirectoryEntry Site in W3SVC.Children)
                //Do some data processing

Running this under XP gives me the folowing error with an HRESULT of -2147463168:

System.Runtime.InteropServices.COMException was unhandled
  Message="Unknown error (0x80005000)"
       at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
       at System.DirectoryServices.DirectoryEntry.Bind()
       at System.DirectoryServices.DirectoryEntry.get_IsContainer()
       at System.DirectoryServices.DirectoryEntries.ChildEnumerator..ctor(DirectoryEntry container)
       at System.DirectoryServices.DirectoryEntries.GetEnumerator()

Googling the various pieces of information in the error lead me to think that it's a pretty generic COM interop error, and I'm out of ideas at this point. Any help is appreciated!

asked on Stack Overflow Feb 3, 2009 by Ralph

2 Answers


Based on the stack trace and reflector it looks like a call to ADsOpenObject is returning E_ADS_BAD_PATHNAME. This error indicates that the path you supplied to the DirectoryEntry class is not valid on the current machine.

If IIS is installed, then it's possible that the IIS provider is not properly installed on your machine.

See this SO question for more details: ADSI will not connect to IIS from XP Workstation

answered on Stack Overflow Feb 3, 2009 by JaredPar • edited May 23, 2017 by Community

Installing IIS fixed it. I hadn't encountered ADSI before writing this little utility, so I didn't realize that software could install their own chunks of ADSI functionality. Thanks for the help!

answered on Stack Overflow Feb 3, 2009 by Ralph

User contributions licensed under CC BY-SA 3.0