0

I want to get the list of files in a folder. This is my code:

var files = Directory.GetFiles("c:\\"); foreach (var f in files) { Console.WriteLine(f); } 

My expectation:

C:\bootmgr C:\BOOTNXT C:\hiberfil.sys C:\pagefile.sys C:\swapfile.sys 

But I got:

C:\a C:\b C:\c 

a, b, c are files in working directory: C:\Code\test\bin\Debug

Is there a way to do it right?

4
  • 4
    Program Files, Program Files (x86), Users, and Windows at the root of the C: drive would not be returned by Directory.GetFiles since they are directories, not files. Commented Nov 23, 2015 at 3:00
  • 1
    Show us the complete code. You might using wrong code to display files. Secondly, you are expecting folders as part of output, however you are calling GetFiles(). Commented Nov 23, 2015 at 3:01
  • 3
    var dirs = new DirectoryInfo("c:\\").GetDirectories(); foreach (var dir in dirs) { Console.WriteLine(dir.Name); } Commented Nov 23, 2015 at 3:02
  • 1
    I think your code must be worked fine. But check your project properties page; open tab: Debug; in Start Options, keep Working directory blank. Try it, hope useful for you. Commented Nov 23, 2015 at 3:41

1 Answer 1

6

Use Directory.GetDirectories method.

Sign up to request clarification or add additional context in comments.

1 Comment

Looks like you edited question and now it says you need files at C drive's root level. Expected output before edit showed directories at C's root.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.