Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract file name from file path name?

Tags:

c#

.net

file-io

I need to move all files from source folder to destination folder. How can I easily extract file name from file path name?

string newPath = "C:\\NewPath";  string[] filePaths = Directory.GetFiles(_configSection.ImportFilePath); foreach (string filePath in filePaths) {   // extract file name and add new path    File.Delete(filePath); } 
like image 432
Captain Comic Avatar asked Oct 21 '10 10:10

Captain Comic


People also ask

How do I separate the filename and path in Python?

split() method in Python is used to Split the path name into a pair head and tail. Here, tail is the last path name component and head is everything leading up to that. In the above example 'file. txt' component of path name is tail and '/home/User/Desktop/' is head.

Does a file path include the file name?

Paths include the root, the filename, or both. That is, paths can be formed by adding either the root, filename, or both, to a directory.


2 Answers

Try the following:

string newPathForFile = Path.Combine(newPath, Path.GetFileName(filePath)); 
like image 160
Pieter van Ginkel Avatar answered Sep 18 '22 08:09

Pieter van Ginkel


Path.GetFileName(filePath) 
like image 37
TalentTuner Avatar answered Sep 18 '22 08:09

TalentTuner