调用 Assembly 获取应用程序名称
我有一个引用实用程序程序集的控制台应用程序 (MyProgram.EXE
)。
在我的实用程序程序集中,我的代码执行以下操作:
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
”
我想要的是“< code>AppName is: 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)
When I call it from MyProgram.EXE
, I receive "AppName is: Utilities.dll
"
What I want is "AppName is: MyProgram.EXE
"
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
GetEntryAssembly()
来获取包含入口点的程序集。更好的方法是使用 System.Environment.CommandLine 属性。
具体来说:
顺便说一句,您想使用
GetFileName
而不是GetDirectoryName
Use
GetEntryAssembly()
instead to get the assembly containing the entry point.The better way to do it is using
System.Environment.CommandLine
property instead.Specifically:
By the way, you want to use
GetFileName
instead ofGetDirectoryName
由于您询问的是 VB.NET,因此您可以从“My”命名空间轻松提取此信息,如下所示:
Since it is VB.NET you were asking about, you can extract this information easily from the 'My' namespace as shown below:
我用:
I use:
所有 C#/VB 环境都支持此功能。
This is supported all over C#/VB environment.
就我而言,我无法访问 My.Application,可能是因为我在全局类中,所以我使用了:
In my case, I didn't have access to My.Application, probably because I was in a global class, so I used: