Current Application Directory Name without full path VB.net

~OSD~ 2,196 Reputation points
2020-12-27T21:36:30.43+00:00

Hi,

I would like to print the current directory name without showing the full path. With current directory I meant the current directory of the VB application from which it's launched. For example; if I launch the application from D:\ParentDir\DynamicDir 3, I should get "DynamicDir 3" and not the D:\ParentDir
I have tried following but I am getting full path:

Label3.Text = AppDomain.CurrentDomain.BaseDirectory

or

Label3.Text = Directory.GetCurrentDirectory()

and same results with following as well:

Label3.Text = My.Application.Info.DirectoryPath
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,741 questions
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2020-12-27T22:02:01.137+00:00

    Hi

    Try this

    Label3.Text = IO.Path.GetFileName(Application.StartupPath)
    
    3 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,441 Reputation points
    2020-12-28T11:51:05.47+00:00

    Hello @~OSD~

    If you want a solution that works for .NET Framework and/or .NET Framework Core consider

    These don't work on .NET Core 5 but work on .NET Framework

    Label3.Text = Path.GetFileName(Application.StartupPath)  
    Label3.Text = Path.GetFileName(AppDomain.CurrentDomain.BaseDirectory)  
    

    This works on both .NET Core 5 and .NET Framework

    Label3.Text = New DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).Name  
    

    The reason why the first two fail on .NET Core is because both have a trailing backslash which is not present on on .NET Core so we would need to use

    Label3.Text = Path.GetFileName(Application.StartupPath.TrimEnd("\"c))  
    

    Or

    Label3.Text = Path.GetFileName(AppDomain.CurrentDomain.BaseDirectory.TrimEnd("\"c))  
    
    4 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.