zoukankan      html  css  js  c++  java
  • VC,C#创建IIS站点,应用程序池 ADSI

    #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();
            }
        }
    }
    
  • 相关阅读:
    android如何播放资源文件夹raw中的视频
    android studio添加三方jar包
    golang自动导入postgresql脚本
    xorm使用pgsql的例子
    python安装psycopg2
    制作u盘启动的工具
    codis须知
    redis3.0自带集群配置
    keepalive配置db层的ha的一些注意点
    phalcon安装笔记
  • 原文地址:https://www.cnblogs.com/ytjjyy/p/2438559.html
Copyright © 2011-2022 走看看