C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
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) 2016 HerongYang.com. All Rights Reserved. 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"); return; } string fromFile = args[0]; string toFile = args[1]; // Copy the file using CopyTo() method FileInfo fromFileObj = new FileInfo(fromFile); fromFileObj.CopyTo(toFile); } }
Compile it with .NET SDK 4.6.1 and test it:
C:\herong>\windows\Microsoft.NET\Framework\v4.0.30319\csc FileInfoCopyFile.cs C:\herong>FileInfoCopyFile \windows\System32\java.exe myjava.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:
Table of Contents
Logical Expressions and Conditional Statements
Visual C# 2010 Express Edition
C# Compiler and Intermediate Language
Compiling C# Source Code Files
MSBuild - Microsoft Build Engine
Public Properties and Methods of FileInfo Class
►FileVersionCopyFile.cs - Testing CopyTo() Method
System.Diagnostics.FileVersionInfo Class
WPF - Windows Presentation Foundation