是否可以从类中获取applicationName?

时间:2012-02-14 00:01:57

标签: c# asp.net-mvc-3

我正在使用asp.net mvc 3中的自定义MembershipUser,我试图动态获取当前应用程序的applicationName。

这是正确还是我合作?

var application = new ApplicationId();

var applicationName = application.Name;

5 个答案:

答案 0 :(得分:4)

我知道这是一个老线程......但是想提供一些与你的问题有关的信息。它存储在会员提供商中。如果您正在实现自己的提供程序,则您将负责从web.config文件中获取它。但无论如何,该值存储在:

    System.Web.Security.Membership.ApplicationName

答案 1 :(得分:2)

最合适的可能是程序集名称,您可以从Assembly实例获取,或者可能只是将应用程序名称推送为appSetting

<configuration>
    <appSettings>
        <add key="ApplicationName" value="MyApp" />
    </appSettings>
</configuration>

string applicationName = ConfigurationManager.AppSettings["ApplicationName"];

答案 2 :(得分:1)

来自

http://www.neowin.net/forum/topic/480752-c-how-to-get-the-applications-name/

您有以下可能性:

'EXE'的产品名称(不是!必要的'EXE'文件的名称)

Application.ProductName; 

'EXE'文件的名称:

Path.GetFileName(Application.ExecutablePath); // 

如果您在非基于表单的课程中。这将使您获得正在执行的程序集名称。

System.Reflection.Assembly.GetExecutingAssembly(); 

我希望这能帮到你

答案 3 :(得分:1)

您可以读取Properties \ AssemblyInfo.cs文件中定义的AssemblyTitleAttribute实例的值。它比在web / app.config文件中定义程序集名称少冗余。

您可以在此处查看如何获取示例中的属性值:

http://msdn.microsoft.com/en-us/library/f2z5sd1c.aspx

答案 4 :(得分:-1)

如果您不反对引用或引用System.Windows.Forms命名空间,那么您可以获取产品名称和其他有用信息,如下所示:

System.Windows.Forms.Application.ProductName;

令人惊讶的是,您无法通过此课程获得标题,说明或其他典型信息 - 您实际上需要反映到程序集中。

using System;
using System.Reflection;


namespace YourNameSpace
{
    public class AssemblyInfoHelper
    {
        private Assembly _Assembly;


        /// <summary>
        /// Whenever we're interested in assembly information, it's 99% of the time the entry assembly
        /// hence used in the default constructor
        /// </summary>
        public AssemblyInfoHelper()
        {
            _Assembly = Assembly.GetEntryAssembly();
        }

        /// <summary>
        /// for cases where we don't want the entry assembly we can supply the desired assembly to interrogate
        /// </summary>
        /// <param name="type"></param>
        public AssemblyInfoHelper(Type type)
        {
            _Assembly = Assembly.GetAssembly(type);
        }

        public AssemblyInfoHelper(string path)
        {
            _Assembly = Assembly.ReflectionOnlyLoadFrom(path);
        }

        private T CustomAttributes<T>()
            where T : Attribute
        {
            object[] customAttributes = _Assembly.GetCustomAttributes(typeof(T), false);

            if ((customAttributes != null) && (customAttributes.Length > 0))
            {
                return ((T)customAttributes[0]);
            }

            throw new InvalidOperationException();
        }

        public string Title
        {
            get
            {
                return CustomAttributes<AssemblyTitleAttribute>().Title;
            }
        }

        public string Description
        {
            get
            {
                return CustomAttributes<AssemblyDescriptionAttribute>().Description;
            }
        }

        public string Company
        {
            get
            {
                return CustomAttributes<AssemblyCompanyAttribute>().Company;
            }
        }

        public string Product
        {
            get
            {
                return CustomAttributes<AssemblyProductAttribute>().Product;
            }
        }

        public string Copyright
        {
            get
            {
                return CustomAttributes<AssemblyCopyrightAttribute>().Copyright;
            }
        }

        public string Trademark
        {
            get
            {
                return CustomAttributes<AssemblyTrademarkAttribute>().Trademark;
            }
        }

        public string AssemblyVersion
        {
            get
            {
                return _Assembly.GetName().Version.ToString();
            }
        }

        public string FileVersion
        {
            get
            {
                FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(_Assembly.Location);
                return fvi.FileVersion;
            }
        }

        public string Guid
        {
            get
            {
                return CustomAttributes<System.Runtime.InteropServices.GuidAttribute>().Value;
            }
        }
    }
}
相关问题