调用程序集以获取应用程序名称 [英] Calling Assembly to get Application Name
问题描述
我有一个引用实用程序程序集的控制台应用程序 (MyProgram.EXE
).
I have a console application (MyProgram.EXE
) that references a Utilities assembly.
在我的实用程序程序集中,我有这样的代码:
In my Utilities assembly, I have code that does:
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
"
When I call it from MyProgram.EXE
, I receive "AppName is: Utilities.dll
"
我想要的是AppName 是:MyProgram.EXE
"
我做错了什么?
推荐答案
改用 GetEntryAssembly()
来获取包含入口点的程序集.
Use GetEntryAssembly()
instead to get the assembly containing the entry point.
更好的方法是使用 System.Environment.CommandLine
属性.
The better way to do it is using System.Environment.CommandLine
property instead.
特别是:
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
By the way, you want to use GetFileName
instead of GetDirectoryName
这篇关于调用程序集以获取应用程序名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!