简体   繁体   English

字符串(路径)中没有文件的文件名存在

[英]Filename from string(path) without file exist

I am trying to get a file name out of a string, but it is possible the file does not exist (yet) when trying to get the file name. 我试图从字符串中获取文件名,但是尝试获取文件名时文件可能不存在(尚未)。 So as far as i know, i can not use the Path.GetFileName Method, or can i somehow still use this method without the file actually existing? 据我所知,我不能使用Path.GetFileName方法,或者我可以在不实际存在文件的情况下以某种方式使用此方法吗?

Example of the string: C:\\cSharp\\test\\001.txt 字符串示例: C:\\cSharp\\test\\001.txt

The length of the file name can variate. 文件名的长度可以变化。 What would be the best approach to try to get the file name? 尝试获取文件名的最佳方法是什么? through regular expressions? 通过正则表达式? maybe some other way? 也许其他方式?

You can use the Path methods even if the file doesn't exist. 即使文件不存在, 也可以使用Path方法。 Actually those Path methods are just string methods. 实际上,那些Path方法只是字符串方法。 So this works: 所以这有效:

string fileName = System.IO.Path.GetFileName(@"C:\cSharp\test\001.txt"); //001.txt

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

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