Retrieving list of filenames

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

I'd like to retrieve a list of files in a directory, but I don't want the
full path included in the name (i.e. I want "myfile.dat" instead of
"d:\foo\myfile.dat"). Directory.GetFiles returns the full path of all the
files in the directory. Is there a way around this problem?
 
You could create a FileInfo object with each file returned, and then
acecss the "Name" property on the FileInfo object.

Louis
 
Dan said:
I'd like to retrieve a list of files in a directory, but I don't want
the full path included in the name (i.e. I want "myfile.dat" instead
of "d:\foo\myfile.dat"). Directory.GetFiles returns the full path of
all the files in the directory. Is there a way around this problem?

Try the System.IO.Path.GetFileName() function. You might also want to check
out the other methods of this class.

- Pete
 
Dan said:
I'd like to retrieve a list of files in a directory, but I don't want
the full path included in the name (i.e. I want "myfile.dat" instead
of "d:\foo\myfile.dat"). Directory.GetFiles returns the full path of
all the files in the directory. Is there a way around this problem?

string [] fullfiles = Directory.GetFile(@"c:\")
string [] files = new string[fullfiles.Length];
for(int i=0; i<fullfiles.Length; i++)
files = Path-GetFileName(fullfiles);

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp
 
Dan said:
I'd like to retrieve a list of files in a directory, but I don't want the
full path included in the name (i.e. I want "myfile.dat" instead of
"d:\foo\myfile.dat"). Directory.GetFiles returns the full path of all the
files in the directory. Is there a way around this problem?

DirectoryInfo dir = new DirectoryInfo( @"C:\temp");

foreach (FileInfo file in dir.GetFiles()) {
Console.WriteLine( file.Name);
}
 
Back
Top