user1931796
user1931796

Reputation: 1

To get filename from path Regex

C:\Users\asus\AppData\Local\Temporary Projects\ConsoleApplication1\bin\Release\sfref.txt

How to split string to get only sfref.txt

Upvotes: 0

Views: 1569

Answers (3)

Tilak
Tilak

Reputation: 30698

You can use FileInfo.Name

new FileInfo(@"C:\Users\asus\AppData\Local\Temporary Projects\ConsoleApplication1\bin\Release\sfref.txt").Name

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

Use Path.GetFileName method:

var path = @"C:\Users\asus\AppData\Local\Temporary Projects\ConsoleApplication1\bin\Release\sfref.txt";
string name = Path.GetFileName(path); // sfref.txt

If you really need to do that with regular expressions (what I do not advice to do):

string name = Regex.Match(path, @"[^\\]*$").Value;

Upvotes: 3

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26143

You don't need regex...

System.IO.Path.GetFilename(fullpath);

Upvotes: 5

Related Questions