A Look at C# File and FileInfo Classes - The File and FileInfo Classes
(Page 2 of 4 )
You may recall from my earlier article that the Directory Class contains only static methods, and we should use this class only when we need to perform one or two operations on a folder. On the other hand, the DirectoryInfo class is used to create an instance that's associated with a specific folder, and we use an instance of this class when we need to perform many operations on the folder.
The same rule applies to the File and the FileInfo classes. We use the static methods of the File Class to perform one operation on the file, and use an instance of the FileInfo class if we are going to perform many operations on the file. So it isn't worth the overhead of creating a FileInfo object just to perform one operation such as checking to see whether a file exists in a directory.
The File class contains many useful methods such as Create(), Move(), Copy(), Delete() and Exists. These methods are self explanatory. There are also methods that return information about the file itself, such as GetAttributes(), SetAttributes, GetCreationTime(), SetCreationTime(), GetLastAccessTime(), SetLastAccessTime(), GetLastWriteTime() and SetLastWriteTime().
The File class contains methods that return stream-based objects such as Create(), OpenText(), AppendText(), Open(), OpenRead(), OpenText() and OpenWrite(). You don't have to worry because we are going to discuss stream-based classes in a separate article. In the meantime we are going to focus on Creating, Moving and Copying.
The methods of the File and FileInfo classes are similar, but they differ in that the methods of the File class are static, so you need to pass more parameters than you would for the methods of the FileInfo instance. You need to do this because it operates on a specific file; for example, the FileInfo.CopyTo() method takes one parameter for the destination path that's used to copy the file, whereas the File.Copy() method takes two parameters for the source path and the destination path. Now let's look at an example of creating files.
using System;
using System.IO;
namespace IOProject
{
class Class1
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Creating the Directory");
DirectoryInfo dirInfo = new DirectoryInfo(
Path.Combine("F:" +
Path.DirectorySeparatorChar,"MyFolder"));
if(dirInfo.Exists == false)
{
dirInfo.Create();
Console.WriteLine("The folder {0} has been created.",
dirInfo.FullName);
}
Console.WriteLine("Creating 3 subdirectories");
for(int i = 1; i < 4; i++)
{
dirInfo.CreateSubdirectory("MyFolder" + i);
}
Console.WriteLine("Creating 3 files in the first
subdirectory");
for(int i = 1; i < 4; i++)
{
FileInfo aFile = new FileInfo(@"F:MyFolderMyFolder1"
+ "File" + i + ".txt");
aFile.Create();
Console.WriteLine("The file {0} has been created at
{1} and the file size = {2}",
aFile.FullName, aFile.CreationTime, aFile.Length);
Console.WriteLine();
}
}
catch(IOException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.ReadLine();
}
}
}
}
Copy the above code, paste it in place of the auto-generated code of Class1.cs in your Console Application Project, then press F5 to run it.

The second line in the try block of the Main() method creates a DirectoryInfo object called dirInfo. Then the if statement checks if the folder already exists. Since it doesn't exist, the Exists property returns false, causing the if statement to evaluate to true and to create the folder using the Create() method.
Then the for statement creates three subdirectories using the method dirInfo.CreateSubdirectory(). Now we have the folder F:MyFolder and three subdirectories inside it, namely MyFolder1, MyFolder2 and MyFolder3. The second for statement creates three files in the directory MyFolder1. We first create the FileInfo object and pass the path to the file; because the file doesn't exist, the aFile.Create() method creates it. Note that if the file already exists, no exception is thrown.
We have used a FileInfo object because we need to retrieve some information on the file like FullName, CreationTime and Length (the size of the file). It is then printed out using the Console.WriteLine() method. The following screen shot shows that the files have been created.

Note that we have used concatenation in order to name the files File1.txt, File2.txt and File3.txt. Let's look at another example.
Next: Moving the files to another directory >>
More C# Articles
More By Michael Youssef