DirectoryInfo.GetFiles returns only the first file name?

  • Thread starter Thread starter rosty
  • Start date Start date
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());
}
}
 
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);
}
 
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.
 
Back
Top