仅从完整文件名 c# 写入相对文件名

Only writing the relative filename from the full file name c#

所以我不知道这是否可行,但下面我在可变路径中有一个文件路径:

string path = @"c:\users\povermyer\documents\visual studio 2013\Projects\DanProject\PNRS
\PNRS.log";

我想做的是让控制台 window 只写路径的最后一部分。例如,我希望控制台 window 的输出为:

The name of this file is PNRS.log

有没有办法简单地做到这一点,还是我必须通过使用目录来做很长的路要走?

使用Path.GetFileName方法。

var fileName = Path.GetFileName(path);

您可以使用:

string path = 
    @"c:\users\povermyer\documents\visual studio 2013\Projects\DanProject\PNRS\PNRS.log";
var fileName = Path.GetFileName(path);

或:

string path = 
    @"c:\users\povermyer\documents\visual studio 2013\Projects\DanProject\PNRS\PNRS.log";

string fileName = path.Split('\').Last();