【C#/C++】使用托管代碼檢測IIS是否安裝并注冊了ASP/ASP.NET等服務(wù)
當(dāng)前位置:點(diǎn)晴教程→知識管理交流
→『 技術(shù)文檔交流 』
介紹我之前關(guān)于使用托管代碼檢測安裝了哪些 .NET Framework 版本和服務(wù)包的文章生成了一些與檢測 Internet 信息服務(wù) (IIS) 是否已安裝以及 ASP 或 ASP.NET 是否已注冊相關(guān)的后續(xù)問題。 由于這是一個托管代碼解決方案,它確實(shí)需要已經(jīng)安裝了 .NET Framework 的一個版本,因此不能保證此類作為安裝過程的一部分工作。如果您需要在安裝過程中可靠地執(zhí)行此操作,則需要考慮使用非托管 C++ 執(zhí)行相同的工作。 背景確定是否安裝了特定版本的 Internet 信息服務(wù)的正確方法是在注冊表中查找以下項(xiàng): HKLM\Software\Microsoft\InetStp\MajorVersion 這是一個 DWORD 值,指示安裝的 IIS 版本(如果存在)。
對于 IIS 5,您可以使用 MinorVersion DWORD 值來確定您是在 Windows 2000 Server 還是 Windows XP Professional 上運(yùn)行。如果 MinorVersion 為 1,則您在 Windows XP Professional 上運(yùn)行。 檢測 IIS 子組件有時,僅僅知道是否安裝了 IIS 是不夠的,您需要確定是否還安裝了特定的子組件。同樣,我們可以轉(zhuǎn)向注冊表以獲取此信息。所有子組件信息都包含在以下注冊表項(xiàng)中: HKLM\Software\Microsoft\Windows\CurrentVersion\Setup\Oc Manager\Subcomponents 此鍵下的所有值都是 DWORD,因此如果值為 1,則安裝該組件。
檢測是否已注冊 ASP 或 ASP.NET為了檢測 ASP 是否在 IIS 中注冊,您可以簡單地查看是否安裝了 ASP 組件 (iis_asp)。但是,對于 ASP.NET,由于有不同版本的 ASP.NET,它變得有點(diǎn)復(fù)雜。我們還可以通過查看以下鍵來使用注冊表獲取此信息:
如果該鍵存在,則該版本的 ASP.NET 已向 IIS 注冊。 使用代碼為了整合檢查所有各種注冊表項(xiàng)并幫助隔離 .NET Framework 和 IIS 的未來版本的更改,
正如你所看到的,這些函數(shù)使用 C#
/// <span class="code-SummaryComment"><summary></span> /// Specifies the .NET Framework versions /// <span class="code-SummaryComment"></summary></span> public enum FrameworkVersion { /// <span class="code-SummaryComment"><summary></span> /// .NET Framework 1.0 /// <span class="code-SummaryComment"></summary></span> Fx10, /// <span class="code-SummaryComment"><summary></span> /// .NET Framework 1.1 /// <span class="code-SummaryComment"></summary></span> Fx11, /// <span class="code-SummaryComment"><summary></span> /// .NET Framework 2.0 /// <span class="code-SummaryComment"></summary></span> Fx20, /// <span class="code-SummaryComment"><summary></span> /// .NET Framework 3.0 /// <span class="code-SummaryComment"></summary></span> Fx30, /// <span class="code-SummaryComment"><summary></span> /// .NET Framework 3.5 (Orcas) /// <span class="code-SummaryComment"></summary></span> Fx35, } /// <span class="code-SummaryComment"><summary></span> /// Specifies the Internet Information Services (IIS) versions /// <span class="code-SummaryComment"></summary></span> public enum InternetInformationServicesVersion { /// <span class="code-SummaryComment"><summary></span> /// Internet Information Services 4 /// <span class="code-SummaryComment"></summary></span> /// <span class="code-SummaryComment"><remarks>Shipped in NT Option Pack for Windows NT 4</remarks></span> IIS4, /// <span class="code-SummaryComment"><summary></span> /// Internet Information Services 5 /// <span class="code-SummaryComment"></summary></span> /// <span class="code-SummaryComment"><remarks>Shipped in Windows 2000 Server and Windows XP Professional</remarks></span> IIS5, /// <span class="code-SummaryComment"><summary></span> /// Internet Information Services 6 /// <span class="code-SummaryComment"></summary></span> /// <span class="code-SummaryComment"><remarks>Shipped in Windows Server 2003</remarks></span> IIS6, /// <span class="code-SummaryComment"><summary></span> /// Internet Information Services 7 /// <span class="code-SummaryComment"></summary></span> /// <span class="code-SummaryComment"><remarks>Shipped in Windows Vista</remarks></span> IIS7, } /// <span class="code-SummaryComment"><summary></span> /// Specifies the Internet Information Services (IIS) versions /// <span class="code-SummaryComment"></summary></span> public enum InternetInformationServicesComponent { /// <span class="code-SummaryComment"><summary></span> /// Internet Information Services Common Files /// <span class="code-SummaryComment"></summary></span> Common, /// <span class="code-SummaryComment"><summary></span> /// Active Server Pages (ASP) for Internet Information Services /// <span class="code-SummaryComment"></summary></span> ASP, /// <span class="code-SummaryComment"><summary></span> /// File Transfer Protocol (FTP) service /// <span class="code-SummaryComment"></summary></span> FTP, /// <span class="code-SummaryComment"><summary></span> /// Internet Information Services Manager /// <span class="code-SummaryComment"></summary></span> InetMgr, /// <span class="code-SummaryComment"><summary></span> /// Internet Data Connector /// <span class="code-SummaryComment"></summary></span> InternetDataConnector, /// <span class="code-SummaryComment"><summary></span> /// Network News Transfer Protocol (NNTP) service /// <span class="code-SummaryComment"></summary></span> NNTP, /// <span class="code-SummaryComment"><summary></span> /// Server-Side Includes /// <span class="code-SummaryComment"></summary></span> ServerSideIncludes, /// <span class="code-SummaryComment"><summary></span> /// Simple Mail Transfer Protocol (SMTP) service /// <span class="code-SummaryComment"></summary></span> SMTP, /// <span class="code-SummaryComment"><summary></span> /// Web Distributed Authoring and Versioning (WebDAV) publishing /// <span class="code-SummaryComment"></summary></span> WebDAV, /// <span class="code-SummaryComment"><summary></span> /// World Wide Web (WWW) service /// <span class="code-SummaryComment"></summary></span> WWW, /// <span class="code-SummaryComment"><summary></span> /// Remote administration (HTML) /// <span class="code-SummaryComment"></summary></span> RemoteAdmin, /// <span class="code-SummaryComment"><summary></span> /// Internet Server Application Programming Interface (ISAPI) for /// Background Intelligent Transfer Service (BITS) server extensions /// <span class="code-SummaryComment"></summary></span> BitsISAPI, /// <span class="code-SummaryComment"><summary></span> /// Background Intelligent Transfer Service (BITS) server extensions /// <span class="code-SummaryComment"></summary></span> Bits, /// <span class="code-SummaryComment"><summary></span> /// FrontPage server extensions /// <span class="code-SummaryComment"></summary></span> FrontPageExtensions, /// <span class="code-SummaryComment"><summary></span> /// Internet printing /// <span class="code-SummaryComment"></summary></span> InternetPrinting, /// <span class="code-SummaryComment"><summary></span> /// ActiveX control and sample pages for hosting Terminal Services /// client connections over the web /// <span class="code-SummaryComment"></summary></span> TSWebClient, } C# 中的完整示例如下所示: C#
關(guān)鍵點(diǎn)bool iis4Installed = InternetInformationServicesDetection.IsInstalled (InternetInformationServicesVersion.IIS4); bool iis5Installed = InternetInformationServicesDetection.IsInstalled (InternetInformationServicesVersion.IIS5); bool iis6Installed = InternetInformationServicesDetection.IsInstalled (InternetInformationServicesVersion.IIS6); bool iis7Installed = InternetInformationServicesDetection.IsInstalled (InternetInformationServicesVersion.IIS7); Console.WriteLine("IIS 4 installed? {0}", iis4Installed); Console.WriteLine("IIS 5 installed? {0}", iis5Installed); Console.WriteLine("IIS 6 installed? {0}", iis6Installed); Console.WriteLine("IIS 7 installed? {0}", iis7Installed); if (iis4Installed || iis5Installed || iis6Installed || iis7Installed) { Console.WriteLine("ASP Registered? {0}", InternetInformationServicesDetection.IsAspRegistered()); Console.WriteLine("ASP.NET 1.0 Registered? {0}", InternetInformationServicesDetection.IsAspNetRegistered(FrameworkVersion.Fx10)); Console.WriteLine("ASP.NET 1.1 Registered? {0}", InternetInformationServicesDetection.IsAspNetRegistered(FrameworkVersion.Fx11)); Console.WriteLine("ASP.NET 2.0 Registered? {0}", InternetInformationServicesDetection.IsAspNetRegistered(FrameworkVersion.Fx20)); // These really don't exist, they are actually the .NET 2.0 version of ASP.NET. Console.WriteLine("ASP.NET 3.0 Registered? {0}", InternetInformationServicesDetection.IsAspNetRegistered(FrameworkVersion.Fx30)); Console.WriteLine("ASP.NET 3.5 Registered? {0}", InternetInformationServicesDetection.IsAspNetRegistered(FrameworkVersion.Fx35)); } 這些 需要注意的是,如果用戶沒有訪問注冊表的適當(dāng)權(quán)限,該函數(shù)將拋出一個異常,該異常將向上冒泡給原始調(diào)用者。這樣做是為了讓調(diào)用者能夠根據(jù)拋出的異常采取不同的操作。 附件:MSNET20.zip 該文章在 2021/6/11 17:48:18 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |