开发者

How to extract file name from file path name?

开发者 https://www.devze.com 2023-01-20 23:10 出处:网络
I need to move all files from source folder to destination folder. How can I easily extract file name from file path name?

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)
{
  开发者_StackOverflow中文版// extract file name and add new path 
  File.Delete(filePath);
}


Try the following:

string newPathForFile = Path.Combine(newPath, Path.GetFileName(filePath));


Path.GetFileName(filePath)


use DirectoryInfo and Fileinfo instead of File and Directory, they present more advanced features.

DirectoryInfo di = 
    new DirectoryInfo("Path");
FileInfo[] files = 
    di.GetFiles("*.*", SearchOption.AllDirectories);

foreach (FileInfo f in files)
    f.MoveTo("newPath");


You may want to try the FileInfo.MoveTo method (code example at the following link):

http://msdn.microsoft.com/en-us/library/system.io.fileinfo.moveto.aspx


You can do it like this:

string newPath = "C:\\NewPath"; 
string[] filePaths = Directory.GetFiles(_configSection.ImportFilePath);  
foreach (string filePath in filePaths)  
{  
   string newFilePath = Path.Combine(newPath, Path.GetFileName(filePath);
   File.Move(filePath, newFilePath);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消