Loading files from directory and add to combo box

2019-08-22 06:49发布

问题:

I'm currently trying to add whatever files that exist in the directory into a combo box.

Dim dir = "‪C:\Users\jason\Desktop\SystemFiles"
For Each file As String In System.IO.Directory.GetFiles(dir)
    cmbTemplateFiles.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file))
Next

When executing this program, it says The given path's format is not supported

  • Do I have to add new header files?
  • Is there anything wrong with my coding?

回答1:

I managed to reproduce the issue.

I suppose it has to do with your dir string containing invisible, strange characters, like ‪

Using your snippet, I managed to display them in VS 2017 by putting a breakpoint on the for each line and hover over the "dir" string. I noticed a question mark where the strange characters occur.

My way of resolving this : I downloaded Notepad++ and copied the dir string in a file and via Encoding -> Ansi I managed to display the strange chars. I removed those and copied the string back into VS. Obviously doing this in a different editor might also work.

Retyping the dir manually might also help. Also refer to What is causing NotSupportedException ("The given path's format is not supported") while using a valid path?