Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy a file in C#

Tags:

c#

.net

file

I want to copy a file from A to B in C#. How do I do that?

like image 747
Daren Thomas Avatar asked Aug 21 '08 13:08

Daren Thomas


People also ask

How do I copy a file to another file?

Select the file you want to copy by clicking on it once. Right-click and pick Copy, or press Ctrl + C . Navigate to another folder, where you want to put the copy of the file. Click the menu button and pick Paste to finish copying the file, or press Ctrl + V .


3 Answers

This should work!

using System.IO;

...

var path = //your current filePath
var outputPath = //the directory where you want your (.txt) file


File.Copy(path,outputPath);
like image 194
Stan van de Bovenkamp Avatar answered Oct 12 '22 23:10

Stan van de Bovenkamp


Without any error handling code:

File.Copy(path, path2);
like image 45
Corey Avatar answered Oct 12 '22 22:10

Corey


The File.Copy method:

MSDN Link

like image 33
Shaun Austin Avatar answered Oct 12 '22 23:10

Shaun Austin