Loading files from directory and add to combo box

谁都会走 提交于 2019-12-13 04:24:28

问题


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?



来源:https://stackoverflow.com/questions/50398078/loading-files-from-directory-and-add-to-combo-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!