Creating Directory in VB.Net by ignoring special or non-supported characters in directory name

~OSD~ 2,201 Reputation points
2023-02-12T11:37:48.8066667+00:00

Hi,

I am creating directory structure something similar to:
ManufacturerName\DeviceModel\DeviceSerialNumber

Dim BIOSInfo As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_BIOS") 'Manufacturer, Serial Number

Dim DeviceInfo As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_ComputerSystem") 'Model

Dim pcVendor As String = "" & queryBIOS("Manufacturer") & ""

Dim pcModel As String = "" & queryDevInfo("Model") & ""

Dim pcSerial As String = "" & queryBIOS("SerialNumber") & ""

Dim folderPath As String = "D:\DevInfo" & pcVendor & "" & pcModel & "" & pcSerial & ""

Problem: On some devices, the manufacture name is comprising of characters on other it's the device model which has invalid character ... which results in failure to create a directory.
Following screenshot shows the results on Fujitsu device, where Manufacturer name has // which is not valid for the creation of directory.

User's image

Similarly, on a Lenovo machine, I have following device model with : which is also not supported:
User's image

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,103 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,448 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,805 questions
0 comments No comments
{count} votes

Accepted answer
  1. LesHay 7,136 Reputation points
    2023-02-12T18:48:49.5233333+00:00

    Hi

    Although there may be other considerations, here is an example to remove invalid characters from a Directory name. There are many other entities that use some sort of Path/naming conventions, but for standard Directory names, this may be OK. If you would rather replace the invalid characters with your own choice, then just replace the 'String.Empty' with the character you want to use.

      Dim fixed1 As String = Fix("Fujitsu // American MegaTrends")
      Dim fixed2 As String = Fix("System x3650 M5: -[8871AC1]-")
    
      Function Fix(s As String) As String
        Dim NotAllowed As String = """\/:|<>*?."
        Dim cc As New List(Of Char)
        For i As Integer = 1 To 31
          NotAllowed &= Chr(i)
        Next
        For Each ch As Char In NotAllowed
          s = s.Replace(ch, String.Empty)
        Next
        Return Trim(s)
      End Function
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. ~OSD~ 2,201 Reputation points
    2023-02-12T11:48:51.4566667+00:00

    ..........

    0 comments No comments

  2. RLWA32 47,696 Reputation points
    2023-02-12T11:55:29.4133333+00:00

    The reserved characters that cannot be used are documented at Naming Files, Paths, and Namespaces. So parse your strings for these reserved characters and replace them with an acceptable substitute or remove them.


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.