IT貓撲網(wǎng):您身邊最放心的安全下載站! 最新更新|軟件分類|軟件專題|手機(jī)版|論壇轉(zhuǎn)貼|軟件發(fā)布

您當(dāng)前所在位置: 首頁網(wǎng)絡(luò)編程.Net編程 → 如何在 asp.net 中使用 WMI 功能,創(chuàng)建web站點(diǎn)等

如何在 asp.net 中使用 WMI 功能,創(chuàng)建web站點(diǎn)等

時(shí)間:2015-06-28 00:00:00 來源:IT貓撲網(wǎng) 作者:網(wǎng)管聯(lián)盟 我要評論(0)

  最近做安裝包是,需要在 .net 程序中調(diào)用 WMI 功能,創(chuàng)建 web 站點(diǎn),虛擬目錄和添加 host 頭部信息,以前曾經(jīng)用腳步做過類似的功能,用.net代碼來做,倒是第一次。

  查找了相關(guān)資料后,主要需要調(diào)用到  System.Management 命名空間下面的一些類來實(shí)現(xiàn):

  創(chuàng)建 Web 站點(diǎn),該函數(shù)返回站點(diǎn)的 ID (這個(gè) ID 是什么意思可以參考下面的文章獲?。篽ttp://blog.crowe.co.nz/archive/2005/12/08/346.aspx )  

CreateWebsite
public static string CreateWebsite(string serverName, string appPoolName, string ip, string pathToRoot, string hostName, string domainName, int port)
        {
            ConnectionOptions options = new ConnectionOptions();
            options.Authentication = AuthenticationLevel.Connect;
            options.EnablePrivileges = true;
            options.Impersonation = ImpersonationLevel.Impersonate;
            ManagementScope scope = new ManagementScope(string.Format(@"file://{0}/root/MicrosoftIISv2, serverName"), options);
            scope.Connect();
            ManagementObject oW3SVC = new ManagementObject(scope, new ManagementPath(@"IIsWebService='W3SVC'"), null);
            ManagementBaseObject[] serverBindings = new ManagementBaseObject[1];
            serverBindings[0] = CreateServerBinding(scope, string.Format("{0}.{1}", hostName, domainName), ip, port);
            ManagementBaseObject inputParameters = oW3SVC.GetMethodParameters("CreateNewSite");
            inputParameters["ServerComment"] = string.Format("{0}.{1}", hostName, domainName);
            inputParameters["ServerBindings"] = serverBindings;
            inputParameters["PathOfRootVirtualDir"] = pathToRoot;
            ManagementBaseObject outParameter = oW3SVC.InvokeMethod("CreateNewSite", inputParameters, null);
            string siteId = Convert.ToString(outParameter.Properties["ReturnValue"].Value).Replace("IIsWebServer='W3SVC/", "").Replace("'", "");
            ManagementObject oWebVirtDir = new ManagementObject(scope,
            new ManagementPath(string.Format(@"IIsWebVirtualDirSetting.Name='W3SVC/{0}/root'", siteId)), null);
            oWebVirtDir.Properties["AppFriendlyName"].Value = string.Format("{0}.{1}", hostName, domainName);
            oWebVirtDir.Properties["AccessRead"].Value = true;
            oWebVirtDir.Properties["AuthFlags"].Value = 5; // Windows 集成驗(yàn)證
            oWebVirtDir.Properties["AccessScript"].Value = true;
            oWebVirtDir.Properties["AuthAnonymous"].Value = true;
            oWebVirtDir.Properties["AppPoolId"].Value = appPoolName;
            oWebVirtDir.Put();
            ManagementObject site = new ManagementObject(scope, new ManagementPath(Convert.ToString(outParameter.Properties["ReturnValue"].Value)), null);
            site.InvokeMethod("Start", null);
            return siteId;
        }

  創(chuàng)建虛擬目錄  

AddVirtualFolder
        public static void AddVirtualFolder(string serverName, string websiteId, string name, string path)
        {
            ManagementScope scope = new ManagementScope(string.Format(@"file://{0}/root/MicrosoftIISV2", serverName));
            scope.Connect();
            string siteName = string.Format("W3SVC/{0}/Root/{1}", websiteId, name);
            ManagementClass mc = new ManagementClass(scope,new ManagementPath("IIsWebVirtualDirSetting"), null);
            ManagementObject oWebVirtDir = mc.CreateInstance();
            oWebVirtDir.Properties["Name"].Value = siteName;
            oWebVirtDir.Properties["Path"].Value = path;
            oWebVirtDir.Properties["AuthFlags"].Value = 5; // Windows 集成驗(yàn)證
            oWebVirtDir.Properties["EnableDefaultDoc"].Value = true;
            // date, time, size, extension, longdate ;
            oWebVirtDir.Properties["DirBrowseFlags"].Value = 0x4000003E;
            oWebVirtDir.Properties["AccessFlags"].Value = 513; // read script
            oWebVirtDir.Put();
            ManagementObject mo = new ManagementObject(scope,new System.Management.ManagementPath("IIsWebVirtualDir='" +siteName + "'"), null);
            ManagementBaseObject inputParameters = mo.GetMethodParameters("AppCreate2");
            inputParameters["AppMode"] = 2;
            mo.InvokeMethod("AppCreate2", inputParameters, null);
            mo = new ManagementObject(scope, new System.Management.ManagementPath("IIsWebVirtualDirSetting='" + siteName + "'"), null);
            mo.Properties["AppFriendlyName"].Value = name;
            mo.Put();
        }

  添加 host 頭  

        public static void AddHostHeader(string serverName, string hostHeader, string ip, int port, string websiteID)
        {
            ManagementScope scope = new ManagementScope(string.Format(@"file://{0}/root/MicrosoftIISV2", serverName));
            scope.Connect();
            string siteName = string.Format("'W3SVC/{0}'", websiteID);
            ManagementObject mo = new ManagementObject(scope,new System.Management.ManagementPath("IIsWebServerSetting=" + siteName), null);
            ManagementBaseObject[] websiteBindings =(ManagementBaseObject[])mo.Properties["ServerBindings"].Value;
            ManagementObject mco = CreateServerBinding(scope, hostHeader, ip, port);
            ManagementBaseObject[] newWebsiteBindings =new ManagementBaseObject[websiteBindings.Length + 1];
            websiteBindings.CopyTo(newWebsiteBindings, 0);
            newWebsiteBindings[newWebsiteBindings.Length - 1] = mco;
            mo.Properties["ServerBindings"].Value = newWebsiteBindings;
            mo.Put();
        }
        private static ManagementObject CreateServerBinding(ManagementScope scope, string hostName, string ip, int port)
        {
            ManagementClass mc = new ManagementClass(scope, new ManagementPath("ServerBinding"), null);
            ManagementObject mco = mc.CreateInstance();
            mco.Properties["Hostname"].Value = hostName;
            mco.Properties["IP"].Value = ip;
            mco.Properties["Port"].Value = port;
            mco.Put();
            return mco;
        }

關(guān)鍵詞標(biāo)簽:asp.net

相關(guān)閱讀

文章評論
發(fā)表評論

熱門文章 誅仙3飛升任務(wù)怎么做-誅仙3飛升任務(wù)流程最新2022 誅仙3飛升任務(wù)怎么做-誅仙3飛升任務(wù)流程最新2022 鐘離圣遺物推薦-原神鐘離圣遺物詞條 鐘離圣遺物推薦-原神鐘離圣遺物詞條 解決方法:應(yīng)用程序“DEFAULT WEB SITE”中的服務(wù)器錯(cuò)誤 解決方法:應(yīng)用程序“DEFAULT WEB SITE”中的服務(wù)器錯(cuò)誤 使用aspnet_regiis.exe 重新注冊.NET Framework 使用aspnet_regiis.exe 重新注冊.NET Framework

相關(guān)下載

    人氣排行 誅仙3飛升任務(wù)怎么做-誅仙3飛升任務(wù)流程最新2022 asp.net表單提交方法GET\POST 在ASP.NET中如何判斷用戶IE瀏覽器的版本 Asp.net中messagebox的實(shí)現(xiàn)方法 Asp.net中的web.config配置 在ASP.NET MVC中實(shí)現(xiàn)大文件異步上傳 asp.net獲取URL和IP地址 FileUpload上傳多文件出現(xiàn)錯(cuò)誤的解決方法