Fernando
Fernando

Reputation: 11

How can I scan for a file in subfolders using C#?

I'm beginner in C#, and I'm trying to search for files in subfolders and show them in a listbox. I have already tried this:

List<string> search = Directory.GetFiles("@C:\\", "*.*", SearchOption.AllDirectories).ToList();

A message from Visual Studio appears: An unhandled exception of type 'System.NotSupportedException' ocurred in mscorlib.dll.

What should I do?

Already grateful!

Upvotes: 0

Views: 224

Answers (3)

Mike Hixson
Mike Hixson

Reputation: 5189

The @ symbol has to go before the double quotes. This indicates that you are not using escaping in the string that follows. When you use this, then you dont need to escape your backslashes. Try changing it to this.

List<string> search = Directory.GetFiles(@"C:\", "*.*", SearchOption.AllDirectories).ToList();

Upvotes: 1

Kelly Robins
Kelly Robins

Reputation: 7288

The NotSupportedException is the result of a bad path... Looks like you put the @ inside the quotes instead of outside.

Upvotes: 3

John Fisher
John Fisher

Reputation: 22721

Read the documentation: http://msdn.microsoft.com/en-us/library/ms127994(v=vs.110).aspx

NotSupportedException: A file or directory name in the path contains a colon (:) or is in an invalid format.

Upvotes: 1

Related Questions