I have a C# progr开发者_如何学Pythonam used to build and deploy a suite of websites.
I would like to programatically interact with IIS so that it changes where the virtual directory for a test system points to based the result of the build.
Is this possible or just crazy talk?
Yes it is possible through WMI. Windows Management Instrumentation is basically an API around windows. You can programmaticly do many things with it.
IIS 6 and lower you need WMI, IIS 7 and greater is a lot simpler as you just use the Microsoft.Web.Administration assembly.
WMI: http://msdn.microsoft.com/en-us/library/aa394582(v=vs.85).aspx
Microsoft.Web.Administration: http://msdn.microsoft.com/en-us/library/ms613523%28v=VS.90%29.aspx
Use Microsoft.Web.Administration
DLL and you can automate IIS 7 and above.
Have a look here.
Which version of IIS? If using 5.1 or 6, you'll have to use com objects to get there. If using 7, you have the option of new classes in .net.
For 5.1 and 6, I tend to use ADSI, rather than WMI, because I've seen a lot of systems where WMI is flaky or not installed correctly. Unsure how this happens, but I've never run across a problem using the ADSI objects. Note: You can actually still use these under IIS 7, but you need to enable the IIS 6 Management Compatibility option, under add/remove windows features.
For more information on IIS7-based objects, check out: Creating Virtual directory in IIS with c#
As others have mentioned, if using IIS7 then use the Microsoft.Web.Administration
managed API.
If this is IIS6 then I'd recommend using System.DirectoryServices
which surfaces the ADSI API's. It's a lot less hassle than using WMI:
Using System.DirectoryServices to Configure IIS
Creating Sites and Virtual Directories Using System.DirectoryServices
If you need to control IIS, for example start/stop sites, recycle app pools then by all means WMI is a better option.
精彩评论