FileVersionCopyFile.cs - Testing CopyTo() Method

This section provides a tutorial example on how to use the FileInfo class to copy an existing file to a new file in the file system with the CopyTo() method.

In the previous section, we learned how to create a FileInfo object to represent a specific file in the file system.

Now let's write a more useful program to copy files using the FileInfo class:

// FileInfoCopyFile.cs
// Copyright (c) 2012 by Dr. Herong Yang,

using System;
using System.IO;
public class FileInfoCopyFile {
   public static void Main(String[] args) {

      // Getting from command line arguments
      if (args.Length < 2) {
         Console.WriteLine("Usage: FileInfoCopyFile fromFile toFile");
      string fromFile = args[0];
      string toFile = args[1];

      // Copy the file using CopyTo() method
      FileInfo fromFileObj = new FileInfo(fromFile);

Compile it with .NET SDK 4.6.1 and test it:


C:\herong>FileInfoCopyFile \windows\System32\java.exe 

C:\herong>myjava.exe -version
java version "1.6.0_30"
Java(TM) SE Runtime Environment (build 1.6.0_30-b12)
Java HotSpot(TM) Client VM (build 20.5-b03, mixed mode, sharing)

What happened in the test above:

Last update: 2015.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

System.IO.FileInfo Class

 What Is FileInfo?

 Public Properties and Methods of FileInfo Class

 Creating FileInfo Objects

FileVersionCopyFile.cs - Testing CopyTo() Method

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Outdated Tutorials


 PDF Printing Version