A Look at C# File and FileInfo Classes - Copying the files to MyFolder3
(Page 4 of 4 )
For this example we need to write and save the following text into the files of the MyFolder2 directory using Notepad:
The files contains some text
Create a file named File1.txt in the MyFolder3 directory but leave it empty. Now let's move to the code.
using System;
using System.IO;
namespace IOProject
{
class Class1
{
static void Main(string[] args)
{
try
{
DirectoryInfo dirInfo2 = new DirectoryInfo
(@"F:MyFolderMyFolder2");
DirectoryInfo dirInfo3 = new DirectoryInfo
(@"F:MyFolderMyFolder3");
Console.WriteLine("Listing the files in MyFolder2:");
FileInfo[] myFolder2Files = dirInfo2.GetFiles();
if(myFolder2Files.Length > 0)
{
foreach(FileInfo aFile in myFolder2Files)
{
Console.WriteLine("File Name: {0}, Size:
{1}",aFile.FullName, aFile.Length);
}
Console.WriteLine("n");
}
else
{
Console.WriteLine("There are no files
MyFolder2...n");
}
Console.WriteLine("Listing the files in MyFolder3:");
FileInfo[] myFolder3Files = dirInfo3.GetFiles();
if(myFolder3Files.Length > 0)
{
foreach(FileInfo aFile in myFolder3Files)
{
Console.WriteLine("File Name: {0}, Size:
{1}",aFile.FullName,aFile.Length);
}
Console.WriteLine("n");
}
else
{
Console.WriteLine("There are no files
MyFolder3...n");
}
Console.WriteLine("Copying the files from MyFolder2 to
MyFolder3");
foreach(FileInfo aFile in myFolder2Files)
{
aFile.CopyTo(@"F:MyFolderMyFolder3" + aFile.Name,
true);
}
Console.WriteLine("The files have been copied to
MyFolder3 directory");
Console.WriteLine("Listing the files in MyFolder3:");
myFolder3Files = dirInfo3.GetFiles();
if(myFolder3Files.Length > 0)
{
foreach(FileInfo aFile in myFolder3Files)
{
Console.WriteLine("File Name: {0}, Size:
{1}",aFile.FullName,aFile.Length);
}
}
else
{
Console.WriteLine("There are no files in the directory
MyFolder3");
}
}
catch(IOException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.ReadLine();
}
}
}
}
As usual, replace the above code with the code of the Class1.cs then press F5 to run the application.

Here are the files in MyFolder3

The code has nothing new except the CopyTo() method, which accepts a destination path and a Boolean value that indicates whether the file overwrites an existing file with the same name or not.
Notice that we have listed the files of MyFolder2. We have found that all the files has the same size (28 Bytes) because they contain the same text. When we have listed the files in MyFolder3, we found the only file that we created (the empty file1.txt file) has a size of 0 Bytes. After the copying operation we found that the file MyFolder3file1.txt has the same size as the other files (28 Bytes) because it has been overwritten by the CopyTo() method.
In my next article I'm going to show you how to write to and read from files using stream-based classes.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |