2009-03-11 6 views
1

Wie finde ich heraus, das .NET Framework eines IIS virtuellen Verzeichnisses in C# verwendet wird. Ich muss es dem Benutzer anzeigen.Wie ermittle ich die ASP.NET-Version eines virtuellen Verzeichnisses oder einer Website mit C#?

using System.DirectoryServices; 
using System.IO; 

private enum IISVersion 
{ 
    IIS5, 
    IIS6 
} 

private void ReadVirtualDirectory(string server, string directory, IISVersion version) 
{ 
    string siteID = string.Empty; 
    string path = string.Empty; 

    switch(verison) 
    { 
     case IISVersion.IIS5: 
      path = string.Format("IIS://{0}/W3SVC/1/Root", server); 

      if(!string.IsNullOrEmpty(directory)) 
      { 
       path = string.Concat(path, "/", directory); 
      } 

      break; 

     case IISVersion.IIS6: 
      path = string.Format("IIS://{0}/W3SVC", server); 

      if(!string.IsNullOrEmpty(directory)) 
      { 
       DirectoryEntry de = new DirectoryEntry(path); 

       foreach(DirectoryEntry child in de.Children) 
       { 
        if(child.Properties["ServerComment"].Value.ToString().ToLower() == directory) 
        { 
         siteID = child.Name; 
         break; 
        } 
       } 

       path = string.Concat(path, "/", siteID); 

       de.Close() 
       de = null; 
      } 

      break; 
    } 

    DirectoryEntry iis = new DirectoryEntry(path); 

    //display iis properties here... 
    //need to display if the virtual directory is running under .NET 1.1 or 2.0 

    iis.Close(); 
    iis = null;  
} 
+0

Netter Code. Was macht es richtig? Was macht es falsch? Beantwortet es nicht die Frage, die du gestellt hast? Wenn nicht, warum nicht? –

+0

Sie möchten wissen, was .NET Framework oder welche Version von IIS? – RSolberg

+0

Ich möchte das .NET Framework kennen. –

Antwort

5

In IIS gibt es keinen harten und schnellen Weg, um herauszufinden, welches ASP.NET-Version von einer Website verwendet wird, oder ‚virtuelle‘ Verzeichnis (Website-Anwendungsordner - die eine das hat Zähne als Ikone).

Der Grund dafür ist, dass IIS und die Metabasisdaten nur über Skriptzuordnungen und nicht über ASP.NET-Versionen bekannt sind (schließlich ist ASP.NET nur eine andere ISAPI-Anwendung). Eine Möglichkeit, die Version von ASP.NET zu bestimmen, ist etwas zu verwenden, wie folgt aus:

using System; 
using System.DirectoryServices; 
namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string path = @"IIS://Localhost/W3SVC/1/root/MyApp"; 
      Console.WriteLine(GetAspNetVersion(path)); 
     } 

     private static string GetAspNetVersion(string path) 
     { 
      using (DirectoryEntry app = new DirectoryEntry(path)) 
      { 
       PropertyValueCollection pvc = app.Properties["ScriptMaps"]; 

       foreach (string scriptMap in pvc) 
       { 
        if (scriptMap.StartsWith(".aspx")) 
        { 
         string[] mapping = scriptMap.Split(','); 
         string scriptProcessor = mapping[1]; 

         // The version numbers come from the path 
         // C:\Windows\Microsoft.NET\Framework\ 
         // which will be present in the script processor 
         // DLL path 
         if (scriptProcessor.Contains("v1.1.4322")) 
         { 
          return "1.1"; 
         } 

         if (scriptProcessor.Contains("v2.0.50727")) 
         { 
          return "2.0"; 
         } 
        } 
       } 
       throw new Exception("Unknown version ASP.NET version."); 
      } 
     } 
    } 
} 

Es ist nicht ideal, aber hat unsere Bedürfnisse als Hoster für unsere Provisioning-Anwendungen/Dienste nicht fehlgeschlagen. Ich vermute, dass das IIS MMC Plug-in eine ähnliche Überprüfung hinter den Kulissen durchführt. Meine eigene detaillierte Untersuchung der Metabasis (Windows 2000 und Windows 2003 dateibasiertes XML) zeigt, dass es keine auffällige Eigenschaft/Attribut gibt, die eine bestimmte ASP.NET-Versionsnummer speichert.

+0

Danke für die Lösung. Ich muss es ändern, um die Framework-Version für jedes virtuelle Verzeichnis auf der Website zu erhalten. –

4

Es ist vielleicht nicht die schönste Methode sein, aber wenn Sie die Ausgabe von aspnet_regiis.exe -lk packen und analysieren, finden Sie alle Informationen, die Sie benötigen. Ein Beispiel für die Ausgabe von einem Sharepoint-VHD:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>aspnet_Regiis -lk 
W3SVC/ 1.1.4322.2407 
W3SVC/1/ROOT/Reports/ 2.0.50727.0 
W3SVC/1/ROOT/ReportServer/  2.0.50727.0 
W3SVC/1619303638/Root/ 2.0.50727.0 
W3SVC/1619303638/Root/_layouts/images/ 1.1.4322.2407 
W3SVC/1619303638/Root/_layouts/inc/  1.1.4322.2407 
W3SVC/1720207907/root/ 2.0.50727.0 
W3SVC/1720207907/root/SharedServices1/ 2.0.50727.0 
W3SVC/1848312571/Root/ 2.0.50727.0 
W3SVC/1848312571/Root/_layouts/images/ 1.1.4322.2407 
W3SVC/1848312571/Root/_layouts/inc/  1.1.4322.2407 
W3SVC/784543535/Root/ 2.0.50727.0 
W3SVC/784543535/Root/_layouts/images/ 1.1.4322.2407 
W3SVC/784543535/Root/_layouts/inc/  1.1.4322.2407 
W3SVC/98413328/Root/ 2.0.50727.0 
W3SVC/98413328/Root/_layouts/images/ 1.1.4322.2407 
W3SVC/98413328/Root/_layouts/inc/  1.1.4322.2407 
+0

Es ist ..... wunderschön! –

Verwandte Themen