Get only filename from a directory

M

Michael

I get all files from a directory an write it into an
array. Then I want get only the file name with file
extension without the full path.

//code
string[] aFilenames;
Directory.GetFiles(comandline.GetParameterValue
("sDirectory"));

for (int i=0; i < aFilenames.Length; i++)
{
// here I need only the file name!
}
//end code

For example:
sDirectory is "c:\tmp\test\"
and in the 'test' folder are two files:
test1.txt
test2.txt

For the sample I get
aFilesnames[0] = c:\tmp\test\test1.txt
aFilesnames[1] = c:\tmp\test\test2.txt

But I need only
aFilesnames[0] = test1.txt
aFilesnames[1] = test2.txt

Should I work with aFilesnames.Remove or something like
that, or which possibilities are there?

Thanks a lot for your assistance.
 
M

Michael

Hi Ignacio,

many thanks for your quick help. It's exactly what I need.

Michael
-----Original Message-----
Hi Michael,

Path.GetFileName is what you are looking for :)

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Michael said:
I get all files from a directory an write it into an
array. Then I want get only the file name with file
extension without the full path.

//code
string[] aFilenames;
Directory.GetFiles(comandline.GetParameterValue
("sDirectory"));

for (int i=0; i < aFilenames.Length; i++)
{
// here I need only the file name!
}
//end code

For example:
sDirectory is "c:\tmp\test\"
and in the 'test' folder are two files:
test1.txt
test2.txt

For the sample I get
aFilesnames[0] = c:\tmp\test\test1.txt
aFilesnames[1] = c:\tmp\test\test2.txt

But I need only
aFilesnames[0] = test1.txt
aFilesnames[1] = test2.txt

Should I work with aFilesnames.Remove or something like
that, or which possibilities are there?

Thanks a lot for your assistance.



.
 

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