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

时间:2012-02-06 16:11:36

标签: .net vb.net

我需要一些帮助才能在DLL中找到VB.Net中App.Path和App.EXEName的等效内容。

感谢您的帮助。

3 个答案:

答案 0 :(得分:11)

根据MSDN(App Object Changes in Visual Basic .NET),两者的替换是

System.Reflection.Assembly.GetExecutingAssembly().Location

它包含完整路径(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

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

答案 1 :(得分:0)

你可以得到它,

Application.ExecutablePath

它包含目录和可执行文件,可以使用System.IO.Path类来分隔它

Dim ExePath = Application.ExecutablePath
Dim ExeFolder = Path.GetDirectoryName(ExePath)  ' which is App.Path in VB6

就是这样。

答案 2 :(得分:0)

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