用WMI可以实现,但是只能对IIS6.0....不可能每台主机都有IIS6吧。。
于是采用Com组件:WbemScripting

可是怎么写也写不好代码。而用JS是好好的。
哪位高手能帮我把JS转成C#?不胜感激!!

JS Code:

// Make connections to WMI, to the IIS namespace on MyMachine, and to the WWW service.
var locatorObj = new ActiveXObject("WbemScripting.SWbemLocator");
var providerObj = locatorObj.ConnectServer("lijialong", "root/MicrosoftIISv2");
var serviceObj = providerObj.Get("IIsWebService='W3SVC'");

// Build binding object, which is a required parameter of the CreateNewSite method.
// Use the SpawnInstance WMI method since we are creating a new instance of an object.
var Bindings = new Array();
Bindings[0] = providerObj.get("ServerBinding").SpawnInstance_();
Bindings[0].IP = "";
Bindings[0].Port = "8383";
Bindings[0].Hostname = "";

// Create the new Web site using the CreateNewSite method of the IIsWebService object.
try {
var strSiteObjPath;
strSiteObjPath = serviceObj.CreateNewSite("MyNewSite", Bindings, "C:\\Inetpub\\Wwwroot");
}
catch(e) {
WScript.Echo("*** Error Creating Site: " + e + " " + e.Number + ", " + e.Description + " ***");
WScript.Quit(1);
}

// strSiteObjPath is in the format of IIsWebServer='W3SVC/1180970907'
// To parse out the absolute path, W3SVC/1180970907, use the SWbemObjectPath WMI object.
var objPath = new ActiveXObject("WbemScripting.SWbemObjectPath");
objPath.Path = strSiteObjPath;
strSitePath = objPath.Keys.Item("");

// var some properties on the root virtual directory which was created by CreateNewSite.
var vdirObj = providerObj.Get("IIsWebVirtualDirSetting='" + strSitePath + "/ROOT'");
vdirObj.AuthFlags = 5; // AuthNTLM + AuthAnonymous
vdirObj.EnableDefaultDoc = true;
vdirObj.DirBrowseFlags = 0x4000003E; // date, time, size, extension, longdate
vdirObj.AccessFlags = 513; // read, script
vdirObj.AppFriendlyName = "Root Application";

// Save the new settings to the metabase
vdirObj.Put_();

// CreateNewSite does not start the server, so start it now.
var serverObj = providerObj.Get(strSiteObjPath);
serverObj.Start;

WScript.Echo("A New site called MyNewSite was created with the path and unique site identification ");

相关文章:

  • 2022-12-23
  • 2021-04-12
  • 2021-09-08
  • 2022-12-23
  • 2021-12-04
  • 2021-08-15
  • 2021-06-25
  • 2021-11-19
猜你喜欢
  • 2021-11-09
  • 2021-10-17
  • 2021-10-09
  • 2021-11-23
  • 2021-07-15
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案