Advertisement
desislava_topuzakova

5. Copy Directory Contents

May 28th, 2022
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. namespace CopyDirectory
  2. {
  3.     using System;
  4.     using System.IO;
  5.  
  6.     public class CopyDirectory
  7.     {
  8.         static void Main()
  9.         {
  10.             string inputPath =  @$"{Console.ReadLine()}";
  11.             string outputPath = @$"{Console.ReadLine()}";
  12.  
  13.             CopyAllFiles(inputPath, outputPath);
  14.         }
  15.  
  16.         public static void CopyAllFiles(string inputPath, string outputPath)
  17.         {
  18.             if (Directory.Exists(outputPath))
  19.             {
  20.                 Directory.Delete(outputPath, true);
  21.             }
  22.             //1. създаваме output директорията
  23.             Directory.CreateDirectory(outputPath);
  24.             //2. взимам всички файлове от input directory
  25.             string [] files = Directory.GetFiles(inputPath);
  26.  
  27.             //3. всеки файл го копирам в output directory
  28.             foreach (string file in files)
  29.             {
  30.                 var fileName = Path.GetFileName(file); //взимаме файла с име
  31.                 var copyDestination = Path.Combine(outputPath, fileName);
  32.                 File.Copy(fileName, copyDestination);
  33.             }
  34.         }
  35.  
  36.        
  37.     }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement