Get program path in VB.NET?
How can I get the absolu开发者_JS百科te path of program I'm running?
For that you can use the Application
object.
Startup path, just the folder, use Application.StartupPath()
Dim appPath As String = Application.StartupPath()
Full .exe path, including the program.exe name on the end:, use Application.ExecutablePath()
Dim exePath As String = Application.ExecutablePath()
Try this: My.Application.Info.DirectoryPath
[MSDN]
This is using the My
feature of VB.NET. This particular property is available for all non-web project types, since .NET Framework 2.0, including Console Apps as you require.
As long as you trust Microsoft to continue to keep this working correctly for all the above project types, this is simpler to use than accessing the other "more direct" solutions.
Dim appPath As String = My.Application.Info.DirectoryPath
For a console application you can use System.Reflection.Assembly.GetExecutingAssembly().Location
as long as the call is made within the code of the console app itself, if you call this from within another dll or plugin this will return the location of that DLL and not the executable.
You can also use:
Dim strPath As String = AppDomain.CurrentDomain.BaseDirectory
You can get path by this code
Dim CurDir as string = My.Application.Info.DirectoryPath
If the path is a drive, a slash will also appear in the path, and this time the use will cause problems. To unify, the best solution is the following command.
Dim FileName As String = "MyFileName"
Dim MyPath1 As String = Application.StartupPath().TrimEnd("\") & "\" & FileName
Dim MyPath2 As String = My.Application.Info.DirectoryPath.TrimEnd("\") & "\" & FileName
Set Your Own application Path
Dim myPathsValues As String
TextBox1.Text = Application.StartupPath
TextBox2.Text = Len(Application.StartupPath)
TextBox3.Text = Microsoft.VisualBasic.Right(Application.StartupPath, 10)
myPathsValues = Val(TextBox2.Text) - 9
TextBox4.Text = Microsoft.VisualBasic.Left(Application.StartupPath, myPathsValues) & "Reports"
I use:
Imports System.IO
Dim strPath as String=Directory.GetCurrentDirectory
精彩评论