Dot-Net

如何以程式方式確定已安裝的 IIS 版本

  • January 21, 2019

以程式方式確定目前安裝的 Microsoft Internet 資訊服務 (IIS) 版本的首選方法是什麼?

我知道可以通過查看 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters 中的 MajorVersion 鍵找到它。

這是推薦的方法嗎,還是有任何更安全或更美觀的方法可供 .NET 開發人員使用?

public int GetIISVersion()
{
    RegistryKey parameters = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\W3SVC\\Parameters");
    int MajorVersion = (int)parameters.GetValue("MajorVersion");

    return MajorVersion;
}

要從 IIS 程序外部辨識版本,一種可能性如下…

string w3wpPath = Path.Combine(
   Environment.GetFolderPath(Environment.SpecialFolder.System), 
   @"inetsrv\w3wp.exe");
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(w3wpPath);
Console.WriteLine(versionInfo.FileMajorPart);

要在執行時從工作程序中辨識它…

using (Process process = Process.GetCurrentProcess())
{
   using (ProcessModule mainModule = process.MainModule)
   {
       // main module would be w3wp
       int version = mainModule.FileVersionInfo.FileMajorPart
   }
}

引用自:https://stackoverflow.com/questions/435050