调用 Assembly 获取应用程序名称

发布于 2024-07-11 23:53:23 字数 479 浏览 6 评论 0原文

我有一个引用实用程序程序集的控制台应用程序 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

成熟的代价 2024-07-18 23:53:23

使用 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

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:

Dim location As String = System.Environment.GetCommandLineArgs()(0)
Dim appName As String = System.IO.Path.GetFileName(location)
Conole.WriteLine("AppName is: {0}", appName)

By the way, you want to use GetFileName instead of GetDirectoryName

孤凫 2024-07-18 23:53:23

由于您询问的是 VB.NET,因此您可以从“My”命名空间轻松提取此信息,如下所示:

My.Application.Info.AssemblyName

Since it is VB.NET you were asking about, you can extract this information easily from the 'My' namespace as shown below:

My.Application.Info.AssemblyName
岁月染过的梦 2024-07-18 23:53:23

我用:

CallingAppName = System.Reflection.Assembly.GetEntryAssembly.GetName().Name

I use:

CallingAppName = System.Reflection.Assembly.GetEntryAssembly.GetName().Name
旧夏天 2024-07-18 23:53:23

所有 C#/VB 环境都支持此功能。

System.IO.Path.GetFileName(Application.ExecutablePath)

This is supported all over C#/VB environment.

System.IO.Path.GetFileName(Application.ExecutablePath)
无可置疑 2024-07-18 23:53:23

就我而言,我无法访问 My.Application,可能是因为我在全局类中,所以我使用了:

AppName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name

In my case, I didn't have access to My.Application, probably because I was in a global class, so I used:

AppName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文