如何从目录中获取文件名,而不是整个路径

我使用下面的方法来获取文件名..

但它返回整个路径….现在我不想得到整个路径..

我只想要文件名而不是整个路径……

我怎么能得到只有文件名而不是整个路径

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

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

任何人都会对此有所帮助…..

非常感谢…

您可以使用GetFileName方法仅提取没有路径的文件名:

 string filenameWithoutPath = Path.GetFileName(filename); 

System.IO.Path是你的朋友:

 var filenames = from fullFilename in Directory.EnumerateFiles(targetdirectory,"backup-*.zip") select Path.GetFileName(fullFilename); foreach (string filename in filenames) { // ... } 

尝试Path.GetFileName(filename)方法

 You can use this, it will give you all file's name without Extension List lstAllFileName = (from itemFile in dir.GetFiles() select Path.GetFileNameWithoutExtension(itemFile.FullName)).Cast().ToList();