Reputation: 300
I need some help to find the equivalent of App.Path and App.EXEName in VB.Net in a DLL.
Thank you for your help.
Upvotes: 5
Views: 20029
Reputation: 1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim path As String = System.IO.Path.GetFullPath(Application.StartupPath & "\test.txt")
Dim lines = File.ReadAllLines(path)
Dim firstLine = lines(0)
Dim fields = firstLine.Split(Microsoft.VisualBasic.ChrW(44))
' TextBox1.Text = ExeFolder
'Id = fields(4)
TextBox2.Text = fields(0)
End Sub
Upvotes: 0
Reputation: 138
You can get it by,
Application.ExecutablePath
It contains both directory and executable file, to separate it, you can use System.IO.Path
class
Dim ExePath = Application.ExecutablePath
Dim ExeFolder = Path.GetDirectoryName(ExePath) ' which is App.Path in VB6
That's it.
Upvotes: 1
Reputation: 172468
According to MSDN (App Object Changes in Visual Basic .NET), the replacement for both is
System.Reflection.Assembly.GetExecutingAssembly().Location
It contains the full path (App.Path
) as well as the file name (App.EXEName
). You can split the information using the helper methods from the Path
class:
' 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
UPDATE (thanks to the commenters): If you are executing this code in a DLL and you want the name of the EXE that called the DLL, you need to use GetEntryAssembly
instead of GetExecutingAssembly
. Note that GetEntryAssembly
might return Nothing
if your DLL was called from an unmanaged EXE.
Upvotes: 14