什么是VB.Net中的App.Path和App.EXEName

rem*_*rde 5 .net vb.net

我需要一些帮助来在DLL中找到VB.Net中的App.Path和App.EXEName的等价物.

谢谢您的帮助.

Hei*_*nzi 12

根据MSDN(Visual Basic .NET中的应用程序对象更改),两者的替换是

System.Reflection.Assembly.GetExecutingAssembly().Location
Run Code Online (Sandbox Code Playgroud)

它包含完整路径(App.Path)以及文件名(App.EXEName).您可以使用类中的辅助方法拆分信息Path:

' Import System.Reflection and System.IO at the top of your class file
Dim location = Assembly.GetExecutingAssembly().Location
Dim appPath = Path.GetDirectoryName(location)       ' C:\Some\Directory
Dim appName = Path.GetFileName(location)            ' MyLibrary.DLL
Run Code Online (Sandbox Code Playgroud)

更新(感谢评论者):如果您在DLL中执行此代码并且您想要调用DLL的EXE的名称,则需要使用GetEntryAssembly而不是GetExecutingAssembly.请注意,如果从非托管EXE调用DLL ,则GetEntryAssembly可能会返回Nothing.