string[] dirs = Directory.GetFiles("G:\\", "*.az", SearchOption.AllDirectories); 

Next I want to copy files

 File.Copy(s1., Path.GetTempPath() + Namer, true); 

I get an error

 указан каталог, а не файл. 

How do I get the file itself?

 foreach (string s1 in dirs) { File.Copy(s1, Path.GetTempPath() + Namer, true); } 

No Name or FullName is available.

  • @AK I need to look in subfolders - Sergey
  • Before copying, you should check with what you are working with: a directory or a file, if the function returns and directory paths. - Vladimir Martyanov
  • Vladimir, no, I also thought right away that the catalogs are returning. However, there are file names. So the usual Path.GetFileName and Path.GetDirectoryName will help to split the full path into the path and file name. - AK
  • How do you plan to handle the situation when the G drive has folders folder1 and folder2, each file contains 123.ag - in theory you need to recreate the entire folder structure in the target folder, right? - AK
  • @ Sergey what is stored in the variable Namer ? is she, as I understand it, static? because I do not see that in the code somewhere changed. - Lolidze

2 answers 2

Something like this:

 string[] fileNames = Directory.GetFiles("G:\\", "*.az", SearchOption.AllDirectories); string newPath = "C:\\"; foreach (var fileName in fileNames) { //string path = Path.GetDirectoryName(fileName); string name = Path.GetFileName(fileName); //Console.WriteLine(path); //Console.WriteLine(name); var sourceName = fileName; var destName = Path.Combine(newPath, name); File.Copy(sourceName, destName); } 

PS The code is simplified, purely to demonstrate the Path.GetFileName and Path.Combine.

I thought here: in fact, if there are two subfolders 1 and 2 in the folder and 123.ag is in each, they will be written in the same place, one will disappear. In an amicable way, you need to recreate the directory structure.

  • @AK - can I rename it immediately after copying? - Sergey
  • @ Sergey There are options. If you initially want to copy with new randomly / guidoobraznyh names, then the chances that such a file will already be negligible. And if you need the original names and only if they match, then rename ... Then there are two options: either check the file name in advance or catch an IOException - according to MSDN this is exactly the situation "the file already exists." I would check the existence of the file, this is more reliable (see the description of the file, there is more than one option). - AK
  • And in general: it is better not to copy, and then rename - and immediately select a name that will not be. I disassembled you into separate variables and you can collect them back in any desired order. - AK
  • I did something like @AK - folders are being created - I just don’t understand how to copy exactly along this path - which is for example: there is .az G: \ boot \ Necessary \ Plugin.az- I recreated the folders along the path temp - G - folders - this is how copy file to the folder in which it was on G - Sergey
  • one
    @ Sergey Press the button on your question to edit, enter your new code at the end of the question - and then I will try to answer. While I can not understand what and how you recreate and what and how you copy - and do not want to answer blindly. - AK
 Directory.EnumerateFiles("G:\\", "*.az", SearchOption.AllDirectories) .AsParallel() .WithDegreeOfParallelism(2) .ForAll(d => File.Copy(d, Path.GetTempPath()+Path.GetFileName(d),true)); 

.....

  string[] Drives = Environment.GetLogicalDrives(); foreach (string drive in Drives) { Directory.EnumerateFiles(drive , "*.az", SearchOption.AllDirectories) .AsParallel() .WithDegreeOfParallelism(2) .ForAll(d => File.Copy(d, Path.GetTempPath() + Path.GetFileName(d), true)); } 

Recursive search

 string[] Drives = Environment.GetLogicalDrives(); foreach(string drive in Drives) Search(drive); ............. static void Search(string sDir) { try { Directory.EnumerateFiles(sDir, "*.az") .AsParallel() .WithDegreeOfParallelism(2) .ForAll(d => File.Copy(d, Path.GetTempPath() + Path.GetFileName(d), true)); foreach(string path in Directory.EnumerateDirectories(sDir)) Search(path); } catch (System.Exception excpt) { //Console.WriteLine(excpt.Message); } } 
  • I do not understand where to add it? - Sergey
  • @ Sergey instead of all your code - Lolidze
  • files are simply copied to temp - is it possible to get the name of the folder where the file itself is located - Sergey
  • does not work this way (Path.GetTempPath () + Namer + "//" + path.Replace ("/", "") .Replace ("\\", "") - Sergey
  • @ Sergey it would be nice if you designated a task. If you want to get a directory, then Path.GetDirectoryName() will help you, if you need a disk without a name, you can write Path.GetDirectoryName() .Replace("G:\\","") - Lolidze