.net 调用程序集以获取应用程序名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/470020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling Assembly to get Application Name
提问by atconway
I have a console application (MyProgram.EXE) that references a Utilities assembly.
我有一个MyProgram.EXE引用 Utilities 程序集的控制台应用程序 ( )。
In my Utilities assembly, I have code that does:
在我的 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)
When I call it from MyProgram.EXE, I receive "AppName is: Utilities.dll"
当我调用它时MyProgram.EXE,我收到“ AppName is: Utilities.dll”
What I want is "AppName is: MyProgram.EXE"
我要的是“ AppName is: MyProgram.EXE”
What am I doing wrong?
我究竟做错了什么?
回答by Mehrdad Afshari
Use GetEntryAssembly()instead to get the assembly containing the entry point.
GetEntryAssembly()改为使用获取包含入口点的程序集。
The better way to do it is using System.Environment.CommandLineproperty instead.
更好的方法是使用System.Environment.CommandLine属性。
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 GetFileNameinstead of GetDirectoryName
顺便说一句,你想使用GetFileName而不是GetDirectoryName
回答by atconway
Since it is VB.NET you were asking about, you can extract this information easilyfrom the 'My' namespace as shown below:
由于您询问的是 VB.NET,因此您可以轻松地从“My”命名空间中提取此信息,如下所示:
My.Application.Info.AssemblyName
回答by Stefano Hac
I use:
我用:
CallingAppName = System.Reflection.Assembly.GetEntryAssembly.GetName().Name
回答by Justin Tolchin
In my case, I didn't have access to My.Application, probably because I was in a global class, so I used:
就我而言,我没有访问 My.Application 的权限,可能是因为我在一个全局类中,所以我使用了:
AppName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name

