This technique works for IIS6 and IIS7 with the “IIS 6 WMI Compatibility* installed. The purpose is changing the directory of a web site in a remote server. The purpose of this action will be clear in a future post, for now only assume that you want to be able to create a piece of c# code that changes directory of a web site in windows server. Here is a test site.
Now if you run this code.
And if you look at site directory again you will get.
So you have changed the Physical path of the application.
The code is really simple, you need to add reference to System.Management to use WMI, then you create a ConnectionOptions class to specify credentials to use. To access MicrosoftIISv2 object it is really important that you set AuthenticationLevel to PacketPrivacy. Then you simply need to query for IIsWebVirtualDirSetting using AppPoolId to find the application you want, then simply use a ManagementObjectSearcher to issue the query, and finally use LINQ to grab the only result, change the Path property and update modification with Put() method.