0

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?
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
tehais
  • 33
  • 6
  • Change your 1st line to `Dim dir As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "systemfiles")` I'm not sure, but I think this may be a bizarre bug. This line above seems to work fine though. – David Wilson May 17 '18 at 18:40
  • The title is wrong, it should be: VB.NET - The given path's format is not supported for System.IO.Directory.GetFiles(dir) – Ole EH Dufour May 17 '18 at 18:59
  • Can you break up your question into different parts... i.e. getting a directory's contents and populating items into a combo box and where you're getting stuck in each step? – KyleMit May 17 '18 at 19:50

1 Answers1

2

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?

Ole EH Dufour
  • 2,968
  • 4
  • 23
  • 48