Home > database >  Illegal characters in path(Read Description)
Illegal characters in path(Read Description)

Time:11-09

I'm currently making a folder with a symbol in its name, so I used this code:

Private Sub ButtonDirectory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDirectory.Click

    FBDPath.ShowDialog()
    TextBox1.Text = FBDPath.SelectedPath & "\◯Folder Safe *by Me*◯"
End Sub

Private Sub ButtonCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCreate.Click
    Dim folderpath As String
    folderpath = TextBox1.Text
    If Directory.Exists(folderpath) Then
        MsgBox("Folder already exist!", vbInformation, "Safe Folder")
    Else
        Directory.CreateDirectory(folderpath)
        MsgBox("succesfull", "Safe Folder")
    End If
End Sub

So, after I start the build, I can choose a folder, but when I click create, that's where the problem arises: "Illegal characters in path."

FYI:

  • the highlighted problem is "Directory.CreateDirectory(folderpath)~"
  • FBDPath is FolderBrowseDialog
  • there are 2 buttons and a textbox.

CodePudding user response:

Because you're allowing the user to type stuff in, you should remove any invalid input:

Dim pathBits = TextBox1.Text.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)

For Each c as Char in Path.GetInvalidPathChars()
  For i = 0 to pathBits.Length - 1
    pathBits(i) = pathBits(i).Replace(c, "_"c)
  Next i
Next c

folderpath = string.Join(Path.DirectorySeparatorChar, pathBits)

CodePudding user response:

You have a asterisks in your file name: "\◯Folder Safe *by Me*◯". With windows file naming you have to avoid using the following characters.

< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)

More info: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file

  • Related