Get executing assembly name from referenced DLL in C#
What is the best way to get the application name (ie MyApplication.exe) of the executing assembly from a referenced class library in C#?
I need to open the application's app.config to retrieve some appSettings variables for the referenced DLL.
If you want to get the current appdomain's config file, then all you need to do is:
ConfigurationManager.AppSettings
....
(this requires a reference to System.Configuration of course).
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
.
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 ConfigurationManager
class.
To get the exact name without versions, etc. use:
string appName = Assembly.GetEntryAssembly().GetName().Name;
Works with .NET v1.1 and later.
链接地址: http://www.djcxy.com/p/21088.html上一篇: 使用app.config和类库
下一篇: 在C#中从引用的DLL执行程序集名称