设为首页 收藏本站
查看: 874|回复: 0

[经验分享] VC,C#创建IIS站点,应用程序池 ADSI

[复制链接]

尚未签到

发表于 2015-8-14 12:54:35 | 显示全部楼层 |阅读模式
#include   <windows.h>
#include   <Iads.h>
#include   <comdef.h>
#include   <tchar.h>
#include   <stdio.h>
#include   <Adshlp.h>
#pragma   comment(lib,"ActiveDS")
#pragma   comment(lib,"adsiid")
#include <iiisext.h>
#include <iisext_i.c>


BOOL   CreateWebServer(LPCTSTR   bindaddress, LPCTSTR   domain, LPCTSTR   DiskPath);
void   main()
{
CoInitialize(NULL);
if(TRUE == CreateWebServer(_T("192.168.105.119:80"), _T("auto"), _T("D:\\release")))/*D:\\sample\\website\\website*/
printf("create   site   ok/n");
else
printf("create   site   failed/n");
CoUninitialize();
system("pause");
}

BOOL   CreateWebServer(LPCTSTR   bindaddress, LPCTSTR   domain, LPCTSTR   pathname)
{
if(bindaddress == NULL || NULL == domain || NULL == pathname)
return   FALSE;
IADsContainer   *pCont = NULL;
IADs   *pAds = NULL;
IADs   *pVrAds = NULL;
IADsServiceOperations   *pSrvOp = NULL;
IDispatch   *pDisp   =   NULL;
IDispatch   *pVrDisp   =   NULL;
_bstr_t   WNumer = "123";
_bstr_t   newBindings = _bstr_t(bindaddress) + ":" + "";
//    _bstr_t   newBindings = _bstr_t(bindaddress) + ":" + domain;
HRESULT   hr;
if(ADsGetObject(L"IIS://localhost/w3svc", IID_IADsContainer, (void **)&pCont) == S_OK)
{
if(pCont->Create(L"IIsWebServer",WNumer, &pDisp) == S_OK)
{
hr = pDisp->QueryInterface(IID_IADs,   (void **)&pAds);
hr = pDisp->QueryInterface(IID_IADsServiceOperations,   (void **)&pSrvOp);
pAds->Put(L"ServerSize", _variant_t(long(1)));
pAds->Put(L"ServerComment", _variant_t(_bstr_t("auto")));
pAds->Put(L"ServerBindings", _variant_t(newBindings));
pAds->SetInfo();
hr = pCont->GetObject(L"IIsWebServer", (WNumer), &pDisp);
if(pDisp->QueryInterface(IID_IADsContainer, (void **)&pCont) == S_OK)
{
if(pCont->Create(L"IIsWebVirtualDir", L"Root", &pVrDisp) == S_OK)
{
hr = pVrDisp->QueryInterface(IID_IADs,   (void **)&pVrAds);
pVrAds->Put(L"AccessRead", _variant_t(true));
//pVrAds->Put(L"AccessWrite", _variant_t(true));
//pVrAds->Put(L"AccessScript", _variant_t(true));
//pVrAds->Put(L"EnableDirBrowsing", _variant_t(true));
pVrAds->Put(L"AccessExecute", _variant_t(true));
pVrAds->Put(L"Path", _variant_t(pathname));
pVrAds->Put(L"AppRoot", _variant_t(pathname));
pVrAds->Put(L"DefaultDoc", _variant_t("index.htm,index.aspx"));
pVrAds->Put(_bstr_t("AppFriendlyName"),_variant_t("auto"));
pVrAds->SetInfo();
pVrAds->Release();
pAds->Release();
pCont->Release();
}

IADs *pADs = NULL;
hr = ADsGetObject( L"IIS://localhost/w3svc/123/root", IID_IADs, (void **)&pADs );
IISApp2 *pApp2 = NULL;
hr = pADs->QueryInterface( IID_IISApp2, (void **)&pApp2 );
IISApp3 *pApp3 = NULL;
hr = pADs->QueryInterface( IID_IISApp3, (void **)&pApp3 );
VARIANT varPool;
VariantInit( &varPool );
varPool.vt = VT_BSTR;
varPool.bstrVal = SysAllocString( L"MyAppPool" );
VARIANT varCreatePool;
VariantInit( &varCreatePool );
varCreatePool.vt = VT_BOOL;
varCreatePool.boolVal = VARIANT_TRUE;
hr = pApp3->AppCreate3(2, varPool, varCreatePool );

if ( pApp3 )
pApp3->Release();
if ( pADs )
pADs->Release();
hr = pSrvOp->Start();
hr = pSrvOp->Release();
}
}
}
IADs *pool = NULL;
hr = ADsGetObject( L"IIS://localhost/W3SVC/AppPools/MyAppPool", IID_IADs, (void **)&pool );

IISApplicationPool  *applicationPool = NULL;
hr = pool->QueryInterface(IID_IISApplicationPool,(void **)&applicationPool );
if(hr==S_OK)
{
applicationPool->Stop();
applicationPool->Recycle();
applicationPool->Start();
printf("start current application pool");

}

return   true;
}

  



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
namespace test
{
class Program
{
const string W3SVC_PATH = "IIS://localhost/W3SVC";
// Access Flags
const int MD_ACCESS_READ = 0x00000001; //Allow read access.
const int MD_ACCESS_SCRIPT = 0x00000200; // Allow script execution.

static void Main(string[] args)
{
CreateWebSite("xxxxxx","d:\\release");
Console.WriteLine("ok");
Console.Read();
}

public static void CreateWebSite(string siteName, string realPath)
{
if (string.IsNullOrEmpty(siteName))
{
throw new NullReferenceException("创建站点的站点名字不能为空");
}
if (string.IsNullOrEmpty(realPath))
{
throw new NullReferenceException("创建站点的站点真是路径不能为空");
}
DirectoryEntry root = new DirectoryEntry(W3SVC_PATH);
//判断站点是否已经存在,如果存在,则删除已有站点
int siteID = GetSiteID(siteName, "localhost");
if (siteID > 0)
{
throw new Exception("要创建的站点已经存在");
}
siteID = getNewWebSiteID();
DirectoryEntry currentWeb =
root.Children.Add(siteID.ToString(), "IIsWebServer");
currentWeb.CommitChanges();
//currentWeb.Properties["Location"].Value = "/LM/W3SVC/" + siteID.ToString();
currentWeb.Properties["AuthFlags"][0] = "0";
currentWeb.Properties["MaxBandwidth"][0] = "1048576";
currentWeb.Properties["MaxConnections"][0] = "10";
currentWeb.Properties["ServerAutoStart"][0] = "true";
currentWeb.Properties["ServerBindings"].Value = ":80:";// + siteName;
currentWeb.Properties["ServerComment"][0] = siteName;

// 添加web虚拟目录
DirectoryEntry virEntry = currentWeb.Children.Add("root", "IIsWebVirtualDir");
virEntry.CommitChanges();
//virEntry.Properties["Location"].Value = "/LM/W3SVC/"+siteID.ToString()+"/root";
virEntry.Properties["AccessFlags"][0] = MD_ACCESS_READ | MD_ACCESS_SCRIPT;
virEntry.Properties["AppFriendlyName"][0] = siteName;
virEntry.Properties["AppIsolated"][0] = "2";
virEntry.Properties["AppRoot"][0] = "/LM/W3SVC/" + siteID.ToString() + "/Root";
virEntry.Properties["AuthFlags"][0] = 1 | 7;// 设置目录的安全性,0表示不允许匿名访问,1为允许,3为基本身份验证,7为windows继承身份验证            
virEntry.Properties["Path"][0] = realPath;  //虚拟目录物理路径
//virEntry.Properties["AccessExecute"][0] = true;
virEntry.Properties["DefaultDoc"][0] = "index.aspx,index.htm";
virEntry.CommitChanges();
currentWeb.CommitChanges();
virEntry.Close();
currentWeb.Close();
root.Close();
}

public static int GetSiteID(string webSiteName, string serverIP)
{
int SiteID = 0;
try
{
DirectoryEntry root = new DirectoryEntry(@"IIS://" + serverIP + "/W3SVC");
foreach (DirectoryEntry Child in root.Children)
{
string WName = Child.Properties["ServerComment"][0].ToString();
if (webSiteName == WName)
{
SiteID = Convert.ToInt32(Child.Name);
return SiteID;
}
WName = "";
}
}
catch
{
SiteID = 0;
}
return SiteID;
}
public static int getNewWebSiteID()
{
using (System.DirectoryServices.DirectoryEntry rootEntry =
new System.DirectoryServices.DirectoryEntry(W3SVC_PATH))
{
int siteID = 1;
//得到现有的站点标识
foreach (System.DirectoryServices.DirectoryEntry entry in rootEntry.Children)
{
if (entry.SchemaClassName == "IIsWebServer")
{
int ID = Convert.ToInt32(entry.Name);
if (ID >= siteID)
{
siteID = ID + 1;
}
}
}
rootEntry.Close();
return siteID;
}
}

public static void CreateNewWebSite(string hostName)
{
//if (!EnsureNewSiteEnavaible(siteInfo.BindString))
//{
//    throw new DuplicatedWebSiteException("已经有了这样的网站了。" + Environment.NewLine + siteInfo.BindString);
//}

string entPath = String.Format("IIS://{0}/w3svc", hostName);
DirectoryEntry rootEntry =new  DirectoryEntry(entPath);

string newSiteNum = "135";
DirectoryEntry newSiteEntry = rootEntry.Children.Add(newSiteNum, "IIsWebServer");
newSiteEntry.CommitChanges();

newSiteEntry.Properties["ServerBindings"].Value = "192.168.105.119:80:" + "";
newSiteEntry.Properties["ServerComment"].Value = "testwetsite";
newSiteEntry.CommitChanges();

DirectoryEntry vdEntry = newSiteEntry.Children.Add("root", "IIsWebVirtualDir");
vdEntry.CommitChanges();

vdEntry.Properties["Path"].Value = "d:/release";
vdEntry.CommitChanges();
}

//DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
//createAppPool("Abcxxx");
//foreach (DirectoryEntry a in appPool.Children)
//{
//    Console.WriteLine(a.Name);
//}
//DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
//foreach (DirectoryEntry dir in root.Children)
//{
//    if (dir.SchemaClassName == "IIsWebServer")
//    {
//        string ww = dir.Properties["ServerComment"].Value.ToString();
//        Console.WriteLine(string.Format("IIS://localhost/W3SVC/{0}/ROOT/;{1}", dir.Name, ww));
//    }
//}

//using (DirectoryEntry root = new DirectoryEntry("WinNT:"))
//{
//    foreach (DirectoryEntry domain in root.Children)
//    {
//        Console.WriteLine("Domain | WorkGroup:\t" + domain.Name);
//        foreach (DirectoryEntry computer in domain.Children)
//        {
//            Console.WriteLine("Computer:\t" + computer.Name);
//        }
//    }
//}

//Console.ReadLine();
static void createAppPool(string AppPoolName)
{
DirectoryEntry newpool;
DirectoryEntry apppools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
newpool = apppools.Children.Add(AppPoolName, "IIsApplicationPool");
newpool.CommitChanges();
}
}
}

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-98971-1-1.html 上篇帖子: .NET获取服务器信息,如服务器版本、IIS等 下篇帖子: iis 6 tuning
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表