呼唤大会获得应用程序名称VB.NET应用程序、名称、大会、NET

2023-09-03 01:07:45 作者:Sorry’请停止爱我

我有一个控制台应用程序( MyProgram.EXE )引用一个实用程序组件。

I have a console application (MyProgram.EXE) that references a Utilities assembly.

在我的实用工具组件,我有code,做:

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是: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