![](/img/trans.png)
[英]Get web application assembly name, regardless of current executing assembly
[英]Calling Assembly to get Application Name
我有一個引用實用程序程序集的控制台應用程序 ( MyProgram.EXE
)。
在我的 Utilities 程序集中,我有執行以下操作的代碼:
Dim asm As Assembly = Assembly.GetExecutingAssembly()
Dim location As String = asm.Location
Dim appName As String = System.IO.Path.GetDirectoryName(location)
Conole.WriteLine("AppName is: {0}", appName)
當我從MyProgram.EXE
調用它時,我收到“ AppName is: Utilities.dll
”
我想要的是“ AppName is: MyProgram.EXE
”
我究竟做錯了什么?
使用GetEntryAssembly()
來獲取包含入口點的程序集。
更好的方法是使用System.Environment.CommandLine
屬性。
具體來說:
Dim location As String = System.Environment.GetCommandLineArgs()(0)
Dim appName As String = System.IO.Path.GetFileName(location)
Conole.WriteLine("AppName is: {0}", appName)
順便說一句,您想使用GetFileName
而不是GetDirectoryName
由於您詢問的是 VB.NET,因此您可以輕松地從“My”命名空間中提取此信息,如下所示:
My.Application.Info.AssemblyName
我使用:
CallingAppName = System.Reflection.Assembly.GetEntryAssembly.GetName().Name
就我而言,我無權訪問 My.Application,可能是因為我在一個全局類中,所以我使用了:
AppName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
這在整個 C#/VB 環境中都受支持。
System.IO.Path.GetFileName(Application.ExecutablePath)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.