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

时间:2023-02-06 10:51:41

i am using below method to get the file names ..

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

but it returns the entire path ....now i dont 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

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

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

would any one pls help on this.....

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

Many thanks...

非常感谢...

4 个解决方案

#1


42  

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

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

string filenameWithoutPath = Path.GetFileName(filename);

#2


10  

System.IO.Path is your friend here:

System.IO.Path是你的朋友:

var filenames = from fullFilename
                in Directory.EnumerateFiles(targetdirectory,"backup-*.zip")
                select Path.GetFileName(fullFilename);

foreach (string filename in filenames)
{
    // ...
}

#3


2  

try Path.GetFileName(filename) method

尝试Path.GetFileName(filename)方法

#4


1  

You can use this, it will give you all file's name without Extension

    List<string> lstAllFileName = (from itemFile in dir.GetFiles()
                                               select Path.GetFileNameWithoutExtension(itemFile.FullName)).Cast<string>().ToList();

#1


42  

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

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

string filenameWithoutPath = Path.GetFileName(filename);

#2


10  

System.IO.Path is your friend here:

System.IO.Path是你的朋友:

var filenames = from fullFilename
                in Directory.EnumerateFiles(targetdirectory,"backup-*.zip")
                select Path.GetFileName(fullFilename);

foreach (string filename in filenames)
{
    // ...
}

#3


2  

try Path.GetFileName(filename) method

尝试Path.GetFileName(filename)方法

#4


1  

You can use this, it will give you all file's name without Extension

    List<string> lstAllFileName = (from itemFile in dir.GetFiles()
                                               select Path.GetFileNameWithoutExtension(itemFile.FullName)).Cast<string>().ToList();