Visual C#: Move multiple files with the same extensions into another directory

后端 未结 2 1855
悲&欢浪女
悲&欢浪女 2020-12-06 13:04

guys. I\'ve got a problem I can\'t solve: I have a 2 folders I choose with folderBrowserDialog and tons of files in source directory I need to move to the target directory.

2条回答
  •  醉梦人生
    2020-12-06 13:26

    First get all the files with specified extension using Directory.GetFiles() and then iterate through each files in the list and move them to target directory.

    //Assume user types .txt into textbox
    string fileExtension = "*" + textbox1.Text;
    
    string[] txtFiles = Directory.GetFiles("Source Path", fileExtension);
    
    foreach (var item in txtFiles)
    {
       File.Move(item, Path.Combine("Destination Directory", Path.GetFileName(item)));
    }
    

提交回复
热议问题