question

OSD-4642 avatar image
0 Votes"
OSD-4642 asked OSD-4642 commented

Current Application Directory Name without full path VB.net

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




dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

$$ANON_USER$$ avatar image
3 Votes"
$$ANON_USER$$ answered OSD-4642 commented

Hi

Try this

 Label3.Text = IO.Path.GetFileName(Application.StartupPath)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you both.

0 Votes 0 ·
karenpayneoregon avatar image
3 Votes"
karenpayneoregon answered karenpayneoregon edited

Hello @OSD-4642

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))


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.