Title here
Summary here
Here’s the translation of the Go code example to Visual Basic .NET, formatted in Markdown suitable for Hugo:
The System.IO.Path
class provides methods to parse and construct file paths in a way that is portable between operating systems; dir/file
on Linux vs. dir\file
on Windows, for example.
Imports System
Imports System.IO
Module FilePaths
Sub Main()
' Path.Combine should be used to construct paths in a
' portable way. It takes any number of arguments
' and constructs a hierarchical path from them.
Dim p As String = Path.Combine("dir1", "dir2", "filename")
Console.WriteLine("p: " & p)
' You should always use Path.Combine instead of
' concatenating / or \ manually. In addition
' to providing portability, Path.Combine will also
' normalize paths by removing superfluous separators
' and directory changes.
Console.WriteLine(Path.Combine("dir1//", "filename"))
Console.WriteLine(Path.Combine("dir1/../dir1", "filename"))
' Path.GetDirectoryName and Path.GetFileName can be used to split a path to the
' directory and the file.
Console.WriteLine("GetDirectoryName(p): " & Path.GetDirectoryName(p))
Console.WriteLine("GetFileName(p): " & Path.GetFileName(p))
' We can check whether a path is absolute.
Console.WriteLine(Path.IsPathRooted("dir/file"))
Console.WriteLine(Path.IsPathRooted("/dir/file"))
Dim filename As String = "config.json"
' Some file names have extensions following a dot. We
' can split the extension out of such names with GetExtension.
Dim ext As String = Path.GetExtension(filename)
Console.WriteLine(ext)
' To find the file's name with the extension removed,
' use GetFileNameWithoutExtension.
Console.WriteLine(Path.GetFileNameWithoutExtension(filename))
' Path.GetRelativePath finds a relative path between a base and a
' target. It returns an empty string if the target cannot
' be made relative to base.
Console.WriteLine(Path.GetRelativePath("a/b", "a/b/t/file"))
Console.WriteLine(Path.GetRelativePath("a/b", "a/c/t/file"))
End Sub
End Module
To run the program, save it as FilePaths.vb
and use the Visual Basic .NET compiler:
$ vbc FilePaths.vb
$ mono FilePaths.exe
p: dir1\dir2\filename
dir1\filename
dir1\filename
GetDirectoryName(p): dir1\dir2
GetFileName(p): filename
False
True
.json
config
t\file
..\c\t\file
Note that in Visual Basic .NET:
System.IO.Path
class instead of filepath
.Path.Combine
is used instead of filepath.Join
.Path.GetDirectoryName
and Path.GetFileName
are used instead of filepath.Dir
and filepath.Base
.Path.IsPathRooted
is used instead of filepath.IsAbs
.Path.GetExtension
is used instead of filepath.Ext
.Path.GetFileNameWithoutExtension
is used instead of strings.TrimSuffix
.Path.GetRelativePath
is used instead of filepath.Rel
.Path.GetRelativePath
method doesn’t throw an exception for invalid paths, it returns an empty string instead.Remember that the actual output may vary depending on the operating system, as path separators and absolute path formats can differ between Windows and Unix-like systems.