A Look at C# File and FileInfo Classes - Moving the files to another directory
(Page 3 of 4 )
The following code moves the files from MyFolder1 directory to MyFolder2. So let's take a look at it.
using System;
using System.IO;
namespace IOProject
{
class Class1
{
static void Main(string[] args)
{
try
{
DirectoryInfo dirInfo = new DirectoryInfo
(@"F:MyFolderMyFolder1");
DirectoryInfo dirInfo2 = new DirectoryInfo
(@"F:MyFolderMyFolder2");
Console.WriteLine("Listing the files in MyFolder2:");
FileInfo[] myFolder2Files = dirInfo2.GetFiles();
if(myFolder2Files.Length > 0)
{
foreach(FileInfo aFile in myFolder2Files)
{
Console.WriteLine(aFile.FullName);
}
}
else
{
Console.WriteLine("There are no files
MyFolder2...n");
}
Console.WriteLine("Moving the files from MyFolder1 to
MyFolder2");
FileInfo[] myFolder1Files = dirInfo.GetFiles();
foreach(FileInfo aFile in myFolder1Files)
{
if(File.Exists(@"F:MyFolderMyFolder2" + aFile.Name))
{
File.Delete(@"F:MyFolderMyFolder2" + aFile.Name);
}
aFile.MoveTo(@"F:MyFolderMyFolder2" + aFile.Name);
}
Console.WriteLine("The files have been moved to
MyFolder2 directory");
Console.WriteLine("Listing the files in MyFolder2:");
myFolder2Files = dirInfo2.GetFiles();
if(myFolder2Files.Length > 0)
{
foreach(FileInfo aFile in myFolder2Files)
{
Console.WriteLine(aFile.FullName);
}
}
else
{
Console.WriteLine("There are no files in the directory
MyFolder2");
}
}
catch(IOException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.ReadLine();
}
}
}
}
Run the above code and you will get the following screen shot. Don't forget to modify the drive letter.

Now the files have been moved to the MyFolder2 directory.

Let's walk through the code. The first two statements of the try block in the Main() method create two DirectoryInfo objects that correspond to the folders MyFolder1 and MyFolder2. The next thing we do is list the files in the MyFolder2 directory (which has no files, but you may have created some files there) by creating a FileInfo array and assigning it a return array value. We call the method dirInfo2.GetFiles(), then we use an if statement to check whether myFolder2Files.Length > 0 and if so, we list the files using a foreach statement. If there are no files, we write that to the Console.
Now we need to move the files from MyFolder1 to MyFolder2. We do that using the FileInfo.MoveTo() instance method that accepts a destination path as a string value. Because we need to move more than one file we use the FileInfo.MoveTo() method inside a foreach statement.
First, we get the files of the MyFolder1 directory using the dirInfo.GetFiles() method and assign it to a FileInfo array, then inside the foreach structure we check to see whether the file already exists in the MyFolder2 directory. If it exists, we delete it using the static method Delete() of the File class.
You may want to create a FileInfo object and then call the Delete() method, but as we said before, this is just one operation that we are going to do on that file (the file that we want to delete in the MyFolder2 directory), so there is no need to create an object. It's sufficient to use the File.Delete() static method to delete the file.
The method MoveTo() moves the file to MyFolder2 with the same file name. Note that you can specify a new name for the file with the MoveTo() method.
We again use the GetFiles() method of the object dirInfo2 to get its files as an array of FileInfo objects. We then assign this array to myFolder2Files variable, and we use it in the foreach statement to list the files. Let's do one more thing.
Next: Copying the files to MyFolder3 >>
More C# Articles
More By Michael Youssef