2009-03-11 7 views
1

Comment puis-je découvrir le framework .NET d'un répertoire virtuel IIS en C#. Je dois l'afficher à l'utilisateur.Comment puis-je déterminer la version ASP.NET d'un répertoire virtuel ou d'un site Web utilisant 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

Code sympa. Que fait-il bien? Qu'est-ce que ça fait mal? Cela ne répond-il pas à la question que vous avez posée? Si non, pourquoi pas? –

+0

vous voulez savoir quel framework .NET ou quelle version d'IIS? – RSolberg

+0

Je veux connaître le framework .NET. –

Répondre

5

Dans IIS, il n'y a aucun moyen dur et rapide pour savoir quelle version ASP.NET est utilisée par un site Web ou un répertoire « virtuel » (site dossier de l'application - une qui a des rouages ​​comme une icône).

La raison en est que IIS et les données de métabase ne connaissent que les maquettes de script et non les versions ASP.NET (après tout, ASP.NET est juste une autre application ISAPI). Une façon de déterminer la version d'ASP.NET est d'utiliser quelque chose comme ce qui suit:

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."); 
      } 
     } 
    } 
} 

Ce n'est pas idéal, mais n'a pas manqué à nos besoins en tant que pour nos applications hoster/services d'approvisionnement. Je suspecte que le plug-in IIS MMC effectue une vérification similaire dans les coulisses. Mon propre examen détaillé de la métabase (XML basé sur un fichier Windows 2000 et Windows 2003) révèle qu'il n'y a pas de propriété/d'attribut visible qui stocke un numéro de version ASP.NET spécifique.

+0

Merci pour la solution. Je vais devoir le modifier pour obtenir la version du framework pour chaque répertoire virtuel du site web. –

4

Il pourrait ne pas être la plus jolie méthode, mais si vous pouvez saisir et analyser la sortie de aspnet_regiis.exe -lk, vous obtiendrez toutes les informations dont vous avez besoin. Un exemple de la sortie d'un 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

C'est ... beau! –

Questions connexes