Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否可以从类中获取applicationName?_C#_Asp.net Mvc 3 - Fatal编程技术网

C# 是否可以从类中获取applicationName?

C# 是否可以从类中获取applicationName?,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我正在asp.net mvc 3中处理一个自定义成员身份用户,并尝试动态获取当前应用程序的applicationName 这是正确的还是我很酷 var application = new ApplicationId(); var applicationName = application.Name; 最合适的可能是程序集名称,您可以从程序集实例中获取该名称,也可能只是将应用程序名称作为应用程序设置推出: <configuration> <appSettings>

我正在asp.net mvc 3中处理一个自定义成员身份用户,并尝试动态获取当前应用程序的applicationName

这是正确的还是我很酷

var application = new ApplicationId();

var applicationName = application.Name;

最合适的可能是程序集名称,您可以从
程序集
实例中获取该名称,也可能只是将应用程序名称作为
应用程序设置推出

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

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

字符串applicationName=ConfigurationManager.AppSettings[“applicationName”];
来自

您有以下几种可能性:

“EXE”的产品名称(不必输入“EXE”文件的名称)

“EXE”文件的名称:

Path.GetFileName(Application.ExecutablePath); // 
如果你在一个非基于表单的类中。这将为您获取正在执行的程序集名称

System.Reflection.Assembly.GetExecutingAssembly(); 

我希望这对您有所帮助

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

您可以在下面的示例中看到如何获取属性值:


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

    System.Web.Security.Membership.ApplicationName

如果您不反对引用或引用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;
            }
        }
    }
}
使用系统;
运用系统反思;
名称空间YourNameSpace
{
公共类AssemblyInfoHelper
{
私人集会;
/// 
///每当我们对程序集信息感兴趣时,99%的时间都是条目程序集
///因此在默认构造函数中使用
/// 
公共AssemblyInfoHelper()
{
_Assembly=Assembly.GetEntryAssembly();
}
/// 
///对于不需要入口程序集的情况,我们可以提供所需的程序集进行查询
/// 
/// 
公共AssemblyInfoHelper(类型)
{
_Assembly=Assembly.GetAssembly(类型);
}
公共AssemblyInfoHelper(字符串路径)
{
_Assembly=Assembly.ReflectionOnlyLoadFrom(路径);
}
私有T CustomAttributes()
其中T:Attribute
{
object[]customAttributes=\u Assembly.GetCustomAttributes(typeof(T),false);
如果((customAttributes!=null)&&(customAttributes.Length>0))
{
返回((T)customAttributes[0]);
}
抛出新的InvalidOperationException();
}
公共字符串标题
{
得到
{
返回CustomAttributes().Title;
}
}
公共字符串描述
{
得到
{
返回CustomAttributes()。说明;
}
}
公共字符串公司
{
得到
{
返回CustomAttributes().公司;
}
}
公共字符串产品
{
得到
{
返回CustomAttributes().产品;
}
}
公共字符串版权
{
得到
{
返回CustomAttributes()。版权所有;
}
}
公共字符串商标
{
得到
{
返回CustomAttributes().商标;
}
}
公共字符串汇编版本
{
得到
{
返回_Assembly.GetName().Version.ToString();
}
}
公共字符串文件版本
{
得到
{
FileVersionInfo fvi=FileVersionInfo.GetVersionInfo(_Assembly.Location);
返回fvi.FileVersion;
}
}
公共字符串Guid
{
得到
{
返回CustomAttributes().Value;
}
}
}
}

定义“应用程序名称”-产品名称?exe文件名?还有什么吗?它是web.config中成员身份/角色/配置文件的一部分,用于在通用数据库设置中创建用户/角色/配置文件之间的关系,其中多个应用程序可以访问相同的用户池。(我想我描述得有点正确):)打得好,我在这里找到了更多关于它的信息,他们似乎同意你的看法:谢谢!“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;
            }
        }
    }
}