Jan 15

In creating a custom NAnt task for a deployment project I've written at work, I needed to gain access to our deployment web server to reset the default web site.  The sample I had that worked was a .vbs file, and I wanted to convert it to C#.  True, I could have worked with System.DirectoryServices and System.Management, and gone through WMI, but I couldn't quite get the security to work (I'm somewhat of a virgin on those 2 namespaces, so I'm sure it could've been done, but being on a timeline, and having a working example, I went with the .vbs code).

So the question, then, was "Is there a C# alternative to GetObject"?  Could I have imported Microsoft.VisualBasic?  Of course.  But I really wanted to find the C# equivelant, for future reference if nothing else.  Enter .NET Reflector.  2 minutes inside the application, and I found the answer.  Marshal.BindToMoniker.  It's called like so:

  • object w3svc = Marshal.BindToMoniker(String.Format("IIS://{0}/w3svc", _Server));
Enjoy the result of my labor.
Tags: | | |