I need to allow a program from each 50 folders to drop in and take a file from there and move it to a shared folder. Direct me please.

private void button1_Click(object sender, EventArgs e) { string path = @"c:\text.txt"; string newPath = @"c:\d\text.txt"; FileInfo fileInf = new FileInfo(path); if (fileInf.Exists) { fileInf.MoveTo(newPath); } } 
  • for folders there is DirectoryInfo, it has EnumerateFiles and EnumerateDirectories methods. This is with regard to direct. Another question - are your 50 folders in the same folder or anywhere? Do you need to take all files from folders or selectively? - rdorn
  • Yes, in one folder and in each folder one file - Jeron
  • and the second question about the files? and add it to your question, under it there is a button to edit that would make changes - rdorn
  • those. the situation looks like this — there are N folders, exactly one file in each folder, and all this wealth needs to be gathered into one folder? Do files have something in common, such as type or name format? - rdorn
  • It is one format. - Jeron

1 answer 1

If the transferred files can be combined with a common name mask, then the solution is trivial.

 var source = new DirectoryInfo("common source dir"); foreach(var file in source.EnumerateFiles("*.txt", SearchOption.AllDirectories)) { file.MoveTo("target path"); } 

selects all text files from folders located inside the specified source folder and moves to the specified target folder. Of course, the ways need to be replaced with normal ones, I just put in the plugs.

  • Gorgeous)) Thank you very much! I haven't worked in c # for a long time, only in java and I got the deadline: |. - Jeron
  • @ NikitaSemyonov happens. Use - rdorn