Dir Sep Char / Volume sep char or hardcoded backslash?

Alec Pointer 20 Reputation points
2023-02-05T12:38:39.0766667+00:00

Hello all expert users

I'm adding a back slash directory separator char at the end if:

If TargetDir.EndsWith(" : ") = False Then TargetDir = TargetDir + " \ "

My vb.net app which targets .net framework 4.0 will only run on Windows 7 to 11 and Server, so should I use the following?

If TargetDir.EndsWith(Path.VolumeSeparatorChar) = False Then TargetDir = TargetDir + Path.DirectorySeparatorChar

Which one recommended for my use?

Thank you for help.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,570 questions
{count} votes

Accepted answer
  1. Reza Aghaei 4,936 Reputation points MVP
    2023-02-05T13:12:28.53+00:00

    It doesn't matter much. If you take a look at source code of .NET Framwork, you see Path.DirectorySeparatorChar and Path.VolumeSeparatorChar are defined as:

    // Platform specific directory separator character.  This is backslash
    // ('\') on Windows, slash ('/') on Unix, and colon (':') on Mac.
    public static readonly char DirectorySeparatorChar = '\\';
    
    // Platform specific volume separator character.  This is colon (':')
    // on Windows and MacOS, and slash ('/') on Unix.  This is mostly
    // useful for parsing paths like "c:\windows" or "MacVolume:System Folder".
    public static readonly char VolumeSeparatorChar = ':';
    

    Considering the fact that you are using .NET Framework 4.X targeting only Windows OS; feel free to use either.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2023-02-05T13:51:17.07+00:00

    Hi

    Maybe using IO.Path can help. It can form safe paths from parameters as needed.

    e.g. Create a Path to a file called AlarmList.xml.

        Dim TargetPath As String = IO.Path.Combine(Application.StartupPath, "Data", "Data2", "Freddy", "AlarmList.xml")
    
    ' result:  C:\Users\lesha\Documents\Projects\AlarmList\AlarmList\bin\Debug\Data\Data2\Freddy\AlarmList.xml
    
    
    0 comments No comments

  2. Dewayne Basnett 1,041 Reputation points
    2023-02-06T14:56:34.25+00:00

    It seems your logic is backwards.... maybe... Not sure what the point of this is.

            If TargetDir.EndsWith(IO.Path.VolumeSeparatorChar) Then
                TargetDir = IO.Path.Combine(TargetDir, IO.Path.DirectorySeparatorChar)
            End If
    
    
    0 comments No comments