当我们在网上下载开源的WEB程序时,有好些程序不能在虚拟目录下运行. 为了让程序运行起来,我们要建Website ,然后设一个主机头,再去修改host文件.写次去找那个目录结构好深的文件感觉到好麻烦,所以写下这个小工具.
小工具用到的主要技术就是对IIS进行操作,网上已经有许多代码,但是都不够完整. 另外程序还将自动的修改你的HOST文件. 在绑定IP的时候格式为 IP:PORT:DOMAIN
创建的程序如下:难点在于如何去找,Properties中所对应的KEY,对于PATH这个KEY;居然是在children下面创建.MS的这种设计模式不知道有什么好处.
string
entPath
=
String.Format(
"
IIS://{0}/w3svc
"
, HostName);
DirectoryEntry rootEntry
=
GetDirectoryEntry(entPath);
//
取得iis路径
string
newSiteNum
=
GetNewWebSiteID();
//
取得新网站ID
DirectoryEntry newSiteEntry
=
rootEntry.Children.Add(newSiteNum,
"
IIsWebServer
"
);
//
增加站点
newSiteEntry.CommitChanges();
//
保存对区域的更改(这里对站点的更改)
newSiteEntry.Properties[
"
ServerBindings
"
].Value
=
domainIP;
newSiteEntry.Properties[
"
ServerComment
"
].Value
=
commentOfWebSite;
newSiteEntry.Properties[
"
AccessRead
"
][
0
]
=
true
;
newSiteEntry.Properties[
"
AccessExecute
"
][
0
]
=
true
;
newSiteEntry.Properties[
"
AppPoolId
"
][
0
]
=
"
DefaultAppPool
"
;

//
newSiteEntry.Properties["AccessWrite"][0] = true;
newSiteEntry.CommitChanges();
DirectoryEntry vdEntry
=
newSiteEntry.Children.Add(
"
root
"
,
"
IIsWebVirtualDir
"
);
vdEntry.CommitChanges();
vdEntry.Properties[
"
Path
"
].Value
=
webPath;
vdEntry.CommitChanges();

vdEntry.Invoke(
"
AppCreate
"
,
1
);
整个程序比较简单,有兴趣的下载代码.
程序下载