如何从目录而不是整个路径获取文件名 [英] How to get file names from the directory, not the entire path

查看:96
本文介绍了如何从目录而不是整个路径获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下方法获取文件名.但是它返回整个路径,我不想获取整个路径.我只需要文件名,而不是整个路径.

I am using the below method to get the file names. But it returns the entire path and I don't want to get the entire path. I want only file names, not the entire path.

如何获取仅文件名而不是整个路径

How can I get that only file names not the entire path

path= c:\docs\doc\backup-23444444.zip

string[] filenames = Directory.GetFiles(targetdirectory,"backup-*.zip");
foreach (string filename in filenames)
{ }

推荐答案

您可以使用

You could use the GetFileName method to extract only the filename without a path:

string filenameWithoutPath = Path.GetFileName(filename);

这篇关于如何从目录而不是整个路径获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆