Using the Web Application version number from an assembly (ASP.NET/C#)

后端 未结 12 1017
我寻月下人不归
我寻月下人不归 2020-12-07 18:39

How do I obtain the version number of the calling web application in a referenced assembly?

I\'ve tried using System.Reflection.Assembly.GetCallingAssembly().GetName

12条回答
  •  甜味超标
    2020-12-07 19:13

    Some info here: http://www.velocityreviews.com/forums/showpost.php?p=487050&postcount=8

    in asp.net 2.0 each page is built into it own assembly, so only the dll the AssemblyInfo.cs is built into will return the correct answer. just add a static method to AssemblyInfo.cs that returns the version info, and call this method from your other pages.

    -- bruce (sqlwork.com)

    But I wrote a simple method to do that:

        public static string GetSystemVersion(HttpServerUtility server)
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(server.MapPath("~/web.config"));
            System.Xml.XmlNamespaceManager ns = new System.Xml.XmlNamespaceManager(doc.NameTable);
            ns.AddNamespace("bla", "http://schemas.microsoft.com/.NetConfiguration/v2.0");
    
            System.Xml.XmlNode node = doc.SelectSingleNode("/bla:configuration/bla:system.web/bla:authentication/bla:forms[@name]", ns);
    
            string projectName = "";
            if (node != null && node.Attributes != null && node.Attributes.GetNamedItem("name") != null)
                projectName = node.Attributes.GetNamedItem("name").Value; //in my case, that value is identical to the project name (projetname.dll)
            else
                return "";
    
            Assembly assembly = Assembly.Load(projectName);
            return assembly.GetName().Version.ToString();
        }
    

提交回复
热议问题