2012-02-14 57 views
0

我正在使用asp.net mvc 3中的自定義MembershipUser,我試圖動態地獲取當前應用程序的applicationName。是否可以從一個類中獲取applicationName?

是正確的還是我coo-coo?

var application = new ApplicationId(); 

var applicationName = application.Name; 
+0

定義「應用程序名稱」 - 產品名稱? exe文件名?別的東西? – hatchet 2012-02-14 00:08:41

+0

它是web.config中Membership/Roles/Profile的一部分,用於在一般數據庫設置中創建用戶/角色/配置文件之間的關係,其中multipul應用程序可以訪問同一用戶池。 (我想我有點描述正確:) :) – skhot 2012-02-14 00:23:02

回答

2

可能是一個最適合的要麼是集名稱,你可以從一個Assembly實例搶,或者只是推出一個應用程序的名稱作爲appSetting

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

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

好的電話,我在這裏發現更多關於它的信息,它似乎像他們同意你:[link](http://stackoverflow.com/questions/7463284/get- web-application-assembly-name-without-of-current-execution-assembly)謝謝! – skhot 2012-02-14 00:18:01

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(); 

我希望這可以幫助您

+0

是「應用程序。ProductName「是使用成員資格,角色和配置文件標識您的應用程序的部分?從這個字符串: ? – skhot 2012-02-14 00:33:30

1

可以讀取屬性\ AssemblyInfo.cs文件中定義的AssemblyTitleAttribute實例的值。它比在web/app.config文件中定義程序集名稱更少冗餘。

你可以看到如何在這裏的範例中得到屬性值:

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

2

我知道這是一個古老的線程...但希望提供一些信息,實際上涉及到你的問題。它存儲在成員資格提供者中。如果你正在實現自己的提供者,你將負責從web.config文件中獲取它。但無論如何,該值存儲在:

System.Web.Security.Membership.ApplicationName 
-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; 
      } 
     } 
    } 
} 
相關問題