从 C# 中引用的 DLL 中获取执行程序集名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/272821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:03:32  来源:igfitidea点击:

Get executing assembly name from referenced DLL in C#

c#.net

提问by Michael Kniskern

What is the best way to get the application name (i.e MyApplication.exe) of the executing assembly from a referenced class library in C#?

从 C# 中引用的类库中获取执行程序集的应用程序名称(即 MyApplication.exe)的最佳方法是什么?

I need to open the application's app.config to retrieve some appSettings variables for the referenced DLL.

我需要打开应用程序的 app.config 来检索引用的 DLL 的一些 appSettings 变量。

采纳答案by Ben Scheirman

If you want to get the current appdomain's config file, then all you need to do is:

如果你想获取当前 appdomain 的配置文件,那么你需要做的就是:

ConfigurationManager.AppSettings....

ConfigurationManager.AppSettings....

(this requires a reference to System.Configuration of course).

(这当然需要对 System.Configuration 的引用)。

To answer your question, you can do it as Ray said (or use Assembly.GetExecutingAssembly().FullName) but I think the problem is easier solved using ConfigurationManager.

要回答您的问题,您可以按照 Ray 所说(或使用Assembly.GetExecutingAssembly().FullName)来做,但我认为使用ConfigurationManager.

回答by Ray Hayes

To get the answer to the question title:

要获得问题标题的答案:

// Full-name, e.g. MyApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
string exeAssembly = Assembly.GetEntryAssembly().FullName;

// or just the "assembly name" part (e.g. "MyApplication")
string exeAssemblyName = Assembly.GetEntryAssembly().GetName().Name;

As mentioned by @Ben, since you mention wanting to get the configuration information, use the ConfigurationManagerclass.

正如@Ben 所提到的,既然您提到想要获取配置信息,请使用ConfigurationManager该类。

回答by Ben Scheirman

You should never couple your libraries to a consumer (in this case Web, WinForm or WCF app). If your library needs configuration settings, then GIVE it to the library.

您永远不应该将您的库与使用者(在本例中为 Web、WinForm 或 WCF 应用程序)耦合。如果您的库需要配置设置,则将其提供给库。

Don't code the library to pull that data from a consumer's config file. Provide overloaded constructors for this (that's what they are for).

不要对库进行编码以从使用者的配置文件中提取该数据。为此提供重载的构造函数(这就是它们的用途)。

If you've ever looked at the ConfigurationManager.AppSettingsobject, it is simply a NameValueCollection. So create a constructor in your library to accept a NameValueCollectionand have your consumer GIVE that data to the library.

如果你曾经看过这个ConfigurationManager.AppSettings对象,它只是一个NameValueCollection. 因此,在您的库中创建一个构造函数以接受 aNameValueCollection并让您的使用者将该数据提供给库。

//Library
public class MyComponent
{
  //Constructor
  public MyComponent(NameValueCollection settings)
  {
     //do something with your settings now, like assign to a local collection
  }
}

//Consumer
class Program
{
  static void Main(string[] args)
  {
    MyComponent component = new MyComponent(ConfigurationManager.AppSettings);
  }
}

回答by Aoi Karasu

To get the exact name without versions, etc. use:

要获得没有版本等的确切名称,请使用:

string appName = Assembly.GetEntryAssembly().GetName().Name;

Works with .NET v1.1 and later.

适用于 .NET v1.1 及更高版本。

回答by A Reynaldos

If you want to read (and display) version number:

如果要读取(和显示)版本号:

  Assembly ass = System.Reflection.Assembly.GetExecutingAssembly();
  AssemblyName assname = ass.GetName();

  Version ver=assname.Version;

Somewhere in application (ie Title block in a Windows form)

应用程序中的某处(即 Windows 窗体中的标题块)

 this.Text = "Your title    Version " + ver;

回答by Jaider

You can get the assembly through the class type...

您可以通过类类型获取程序集...

typeof(MyClass).Assembly

回答by James Harcourt

If you want the name of the parent EXE and not the referenced DLL assembly - you will need to use this:

如果您想要父 EXE 的名称而不是引用的 DLL 程序集 - 您需要使用以下命令:

Assembly.GetEntryAssembly().GetName().Name

This will return the EXE name (minus the .EXE part).

这将返回 EXE 名称(减去 .EXE 部分)。

Using GetExecutingAssembly() is not right as per the OP's question (first paragraph of it!) as it will return the DLL name.

根据 OP 的问题(第一段!),使用 GetExecutingAssembly() 是不正确的,因为它会返回 DLL 名称。

回答by Gerardo Aguero

For the short name of an assembly from a class instance:

对于来自类实例的程序集的短名称:

Me.GetType ().Assembly.GetName().Name