从目录加载文件并添加到组合框 [英] Loading files from directory and add to combo box

查看:97
本文介绍了从目录加载文件并添加到组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将目录中存在的所有文件添加到组合框中.

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

执行该程序时,显示The given path's format is not supported

  • 我必须添加新的头文件吗?
  • 我的编码有问题吗?

推荐答案

我设法重现了该问题.

我想这与您的dir字符串有关,该dir字符串包含不可见的奇怪字符,例如‪

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

使用您的代码段,我设法在VS 2017中显示它们,方法是在每行的上放置一个断点,并将鼠标悬停在"dir"字符串上.我注意到一个问号,其中出现了奇怪的字符.

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.

解决这个问题的方法:我下载了Notepad ++并将dir字符串复制到了文件中,并通过Encoding-> Ansi设法显示了奇怪的字符.我删除了这些并将字符串复制回VS.显然,在其他编辑器中执行此操作也可以.

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.

手动重新输入目录也可能会有所帮助. 另请参阅引起NotSupportedException的原因(使用有效路径时不支持给定路径的格式")?

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?

这篇关于从目录加载文件并添加到组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆