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.

Interested?

VB.NET development for your project?

I work with VB.NET and C# on a daily basis. Whether it's file system operations or full applications – just send me a message.

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

How do I get the application path in VB.NET?

The most reliable way is AppContext.BaseDirectory. It works across all .NET project types, including Windows Forms, WPF and .NET Core.

What is the difference between AppContext.BaseDirectory and Application.StartupPath?

Application.StartupPath is only available in Windows Forms projects. AppContext.BaseDirectory works in all .NET project types and is the modern recommendation.

Why should I avoid My.Application.Info.DirectoryPath?

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.

Can I use a relative path in VB.NET?

Yes. With .\file.txt or File.WriteAllText("file.txt", ...) you reference files relative to the execution directory without needing the absolute path.

Downloads

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Robert Skibbe
Datenschutz-Übersicht

Diese Website verwendet Cookies, damit wir dir die bestmögliche Benutzererfahrung bieten können. Cookie-Informationen werden in deinem Browser gespeichert und führen Funktionen aus, wie das Wiedererkennen von dir, wenn du auf unsere Website zurückkehrst, und hilft unserem Team zu verstehen, welche Abschnitte der Website für dich am interessantesten und nützlichsten sind.