简体   繁体   中英

Illegal characters in path(Read Description)

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.

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM