
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to copy files into a directory in C#?
To Copy a file, C# provides a method File. Copy
File. Copy has 2 overloads
Copy(String, String) -Copies an existing file to a new file. Overwriting a file of the same name is not allowed.
Copy(String, String, Boolean) Copies an existing file to a new file. Overwriting a file of the same name is allowed.
Directory.GetFiles returns the names of all the files (including their paths) that match the specified search pattern, and optionally searches subdirectories.
Example
static void Main (string[] args) { string rootPath = @"C:\Users\Koushik\Desktop\TestFolder\TestFolderMain1"; var searchSourceFolder = Directory.GetFiles(rootPath, "*.*", SearchOption.TopDirectoryOnly); Console.WriteLine("-------------Source Folder-------------"); foreach (string file in searchSourceFolder){ Console.WriteLine(file); } string destinationFolder = @"C:\Users\Koushik\Desktop\TestFolder\TestFolderMain2\"; var destinationFolderFiles = Directory.GetFiles(destinationFolder, "*.*", SearchOption.TopDirectoryOnly); Console.WriteLine("-------------Destination Folder Before Copying-------------"); foreach (string file in destinationFolderFiles){ Console.WriteLine(file); } string[] files = Directory.GetFiles(rootPath); foreach (string file in files){ File.Copy(file, $"{destinationFolder}{ Path.GetFileName(file) }"); } Console.WriteLine("-------------After Copying-------------"); var destinationFolderAfterCopyingFiles = Directory.GetFiles(destinationFolder, "*.*", SearchOption.TopDirectoryOnly); foreach (string file in destinationFolderAfterCopyingFiles){ Console.WriteLine(file); } Console.ReadLine (); }
Output
-------------Source Folder------------- C:\Users\Koushik\Desktop\TestFolder\TestFolderMain1\TestFolderMain1.txt -------------Destination Folder Before Copying------------- C:\Users\Koushik\Desktop\TestFolder\TestFolderMain2\TestFolderMain2.txt -------------After Copying------------- C:\Users\Koushik\Desktop\TestFolder\TestFolderMain2\TestFolderMain1.txt C:\Users\Koushik\Desktop\TestFolder\TestFolderMain2\TestFolderMain2.txt
- Related Questions & Answers
- How to copy files to a new directory using Python?
- How to copy a file, group of files, or directory in Linux?
- How to load files from asset directory into WebView in Android?
- How to Copy the entire contents of a directory in C#?
- How to copy multiple files in PowerShell using Copy-Item?
- How to copy a String into another String in C#
- How to get all the files, sub files and their size inside a directory in C#?
- How to rename multiple files in a directory in Python?
- How to delete multiple files in a directory in Python?
- How to Copy files or Folder without overwriting existing files?
- How to delete all files in a directory with Python?
- How to list all files in a directory using Java?
- How to unzip all zipped files in a Linux directory?
- How to list down all the files available in a directory using C#?
- How to list the hidden files in a directory in Java?
Advertisements