繁体   English   中英

如何从C#中的文件名中删除目录字符串

[英]How to remove directory string from a file name in c#

这是我的代码

string[] games = System.IO.Directory.GetFiles(path, "*.exe");            
listBox1.Items.AddRange(games);

现在,它以字符串形式打印出整个目录。 我只想获取文件名,所以要获取目录的最后一部分。 我该怎么做?

您应该使用Path.GetFileName方法。

您可以使用FileInfo类来获取该信息:

string[] games = Directory.GetFiles(path, "*.exe")
                          .Select(x => new FileInfo(x).Name)
                          .ToArray();     

你可以这样

string[] games = System.IO.Directory.GetFiles(path, "*.exe");
foreach (var g in games)
{
   listBox1.Items.Add(Path.GetFileName(g));
}

试试这个

file_name = Path.GetFileName (your path);

GetFileName将从路径字符串重新运行文件名

使用System.IO.Path.GetFileName(...) 请同时阅读MSDN文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM