VB.NET Application Path: All Methods Compared (2026)
There are several ways to get the application path in VB.NET. The most reliable solution for modern .NET projects is AppContext.BaseDirectory. In this post you’ll see all approaches compared: from relative paths to Application.StartupPath and the legacy My namespace.
Inhaltsverzeichnis
The legacy approach
The following approach is considered outdated and should no longer be used:
My.Application.Info.DirectoryPath
The My.Application.Info object provides various information about the application. The problem with the My namespace is that it’s only available in VB.NET and can’t be accessed from other .NET languages without workarounds. For new projects, use one of the alternatives below.
The simplest approach
The simplest way to handle the VB.NET application path is to use a relative path. This way you skip dealing with the absolute path entirely:
File.WriteAllText(".\Sample.txt", "Contents..")
This writes a file called Sample.txt with the given content to the current directory of the application.
More on file operations: VB.NET write text file and VB.NET read text file.
The Windows Forms approach
In a Windows Forms context, you can use:
Application.StartupPath
The more verbose variant via ExecutablePath:
System.IO.Path.GetDirectoryName(Application.ExecutablePath)
There’s also the option of using:
Environment.CurrentDirectory
CurrentDirectory can be changed at runtime and may no longer return the expected result. I recommend against using it.
The recommended approach
AppContext.BaseDirectory is the recommended solution. It works in Windows Forms, WPF and .NET Core and always returns the directory of the executing assembly.
AppContext.BaseDirectory
Want to open a file directly from the path? Take a look at VB.NET open PDF file.
FAQ
The most reliable way is AppContext.BaseDirectory. It works across all .NET project types, including Windows Forms, WPF and .NET Core.
Application.StartupPath is only available in Windows Forms projects. AppContext.BaseDirectory works in all .NET project types and is the modern recommendation.
The My namespace is VB.NET-specific and can’t be accessed from other .NET languages. It’s considered legacy and should not be used in new projects.
Yes. With .\file.txt or File.WriteAllText("file.txt", ...) you reference files relative to the execution directory without needing the absolute path.