DirectoryInfo.GetFiles returns only the first file name?

R

rosty

i'm trying to get the list of files in the current directory, however it
appears that GetFiles returns only the first file.
the msdn says that it "Returns a file list from the current directory."
what am i doing wrong?
many TIA

static void Main(string[] args)
{
try
{
DirectoryInfo dir = new DirectoryInfo(".");
Console.WriteLine("File list :{0}",dir.GetFiles("*.*"));
// Console.WriteLine("File list: {0}",dir.GetFiles());
}
catch(Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
 
J

Jon Skeet [C# MVP]

rosty said:
i'm trying to get the list of files in the current directory, however it
appears that GetFiles returns only the first file.
the msdn says that it "Returns a file list from the current directory."
what am i doing wrong?
many TIA

static void Main(string[] args)
{
try
{
DirectoryInfo dir = new DirectoryInfo(".");
Console.WriteLine("File list :{0}",dir.GetFiles("*.*"));
// Console.WriteLine("File list: {0}",dir.GetFiles());
}
catch(Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}

The return value from dir.GetFiles is an array, and you're just
printing out what happens when you call ToString on the array. You want
to print out each item. For instance:

DirectoryInfo dir = new DirectoryInfo(".");
FileInfo[] files = dir.GetFiles("*.*");
Console.WriteLine ("Fie list:");
foreach (FileInfo file in files)
{
Console.WriteLine (file.Name);
}
 
M

Miha Markic [MVP C#]

Hi rosty,

It returns all files it just doesn't show them in the way you are outputing
it - check dir.GetFiles("*.*").Length property.
You should iterate through returned array (GetFiles returns a FileInfo[]
array) and build a string by yourself if you want to output file names.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top