Reputation: 167
I'm working on a custom MembershipUser in asp.net mvc 3 and I'm trying to dynamically get the applicationName of the current application.
Is it correct or am I coo-coo?
var application = new ApplicationId();
var applicationName = application.Name;
Upvotes: 0
Views: 6065
Reputation: 8503
You can read the value of the AssemblyTitleAttribute
instance defined in the Properties\AssemblyInfo.cs file. It is less redundant than defining the assembly name in the web/app.config file.
You can see how to get the attribute value in the example here:
http://msdn.microsoft.com/en-us/library/f2z5sd1c.aspx
Upvotes: 1
Reputation: 1390
If you are not averse to referring to or referencing the System.Windows.Forms namespace, then you can get to the product name and other useful information like so:
System.Windows.Forms.Application.ProductName;
Surprisingly, you cannot get the title, description or other typical information via this class - you actually need to reflect into the assembly.
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;
}
}
}
}
Upvotes: 0
Reputation: 1825
I know this is an old thread...but wanted to provide some info that actually pertains to your question. It's stored within the membership provider. If you're implementing your own provider, you'll be responsible to grab it from the web.config file. But anyway, the value is stored in:
System.Web.Security.Membership.ApplicationName
Upvotes: 4
Reputation: 4082
From
http://www.neowin.net/forum/topic/480752-c-how-to-get-the-applications-name/
You have the following possibilities:
Product name of the 'EXE' (not! necessary the name of the 'EXE' file)
Application.ProductName;
Name of the 'EXE' file:
Path.GetFileName(Application.ExecutablePath); //
If you are in a Non form-based class. This get you the executing assembly name.
System.Reflection.Assembly.GetExecutingAssembly();
I hope that this help you
Upvotes: 1
Reputation: 61589
Probably a best fit would either be the assembly name, which you can grab from an Assembly
instance, or perhaps just push out an application name as an appSetting
:
<configuration>
<appSettings>
<add key="ApplicationName" value="MyApp" />
</appSettings>
</configuration>
string applicationName = ConfigurationManager.AppSettings["ApplicationName"];
Upvotes: 3