0

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!

Fernando
  • 11
  • 4

3 Answers3

3

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

Kelly Robins
  • 7,168
  • 6
  • 43
  • 66
  • Yes, really! I tried to make what you told me, and everything worked just fine! But an error occurs when I try to access the C:\ directory, now an "An unhandled exception of type 'System.UnauthorizedAccessException' ocurred in mscorlib.dll". If I try to access some folders\subfolders that I created just like: "C:\FOLDERTEST\SUBFOLDERTEST" there is no problem... – Fernando Jun 15 '14 at 16:17
  • @user3741358 Glad that helped! The other error is the result of your system's security settings. Your application doesn't have permission to access some of the directories on C by default... For example if I try on mine I get denied at documents and settings. You may want to take a look here for a possible workaround - http://stackoverflow.com/questions/172544/ignore-folders-files-when-directory-getfiles-is-denied-access?rq=1; – Kelly Robins Jun 15 '14 at 20:17
1

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.

John Fisher
  • 22,355
  • 2
  • 39
  • 64
1

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();
Mike Hixson
  • 5,071
  • 1
  • 19
  • 24
  • Yes, really! I tried to make what you told me, and everything worked just fine! But an error occurs when I try to access the C:\ directory, now an "An unhandled exception of type 'System.UnauthorizedAccessException' ocurred in mscorlib.dll". If I try to access some folders\subfolders that I created just like: "C:\FOLDERTEST\SUBFOLDERTEST" there is no problem... – Fernando Jun 15 '14 at 16:18
  • You have a permissions issue here. On the machine you are using, your windows account does not have access to c:\ or one of its sub-directories. Do you have administrator access on your computer? If so, try right clicking on the icon you use to open visual studio and select "run as administrator". If not, you need to make sure your account has access to all the folders that you are scanning from the parent and all its subs. – Mike Hixson Jun 15 '14 at 16:59