string path = @'C:My Folder';
try {
//---if directory does not exists---
if (!Directory.Exists(path)) {
//---create the directory---
Directory.CreateDirectory(path);
//---set the current directory to C:My Folder---
Directory.SetCurrentDirectory(path);
//---creates subdirectories---
//---c:My FolderSubdir1---
Directory.CreateDirectory('Subdir1');
//---c:My FolderSubdir2---
Directory.CreateDirectory('Subdir2');
}
//---set the current directory to C:My Folder---
Directory.SetCurrentDirectory(path);
//---print out some info about the directory---
Console.WriteLine(Directory.GetCurrentDirectory());
Console.WriteLine(Directory.GetCreationTime(path));
//---get and print all the subdirectories---
string[] subDirs = Directory.GetDirectories(path);
foreach (string subDir in subDirs)
Console.WriteLine(subDir);
//---get the parent of C:My folder---
DirectoryInfo parent = Directory.GetParent(path);
if (parent.Exists) {
//---prints out C:---
Console.WriteLine(parent.FullName);
}
//---creates C:My FolderSubdir3---
Directory.CreateDirectory('Subdir3');
//---deletes C:My FolderSubdir3---
Directory.Delete('Subdir3');
} catch (IOException ex) {
Console.WriteLine(ex.Message);
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
As you can see, most of the methods in the Directory
class require you to specify the directory you are working with. If you like to specify the directory path by using relative path names, you need to set the current working directory using the SetCurrentDirectory()
method; if not, the default current directory is always the location of your program. Also, notice that some methods (such as GetParent()
) still return DirectoryInfo
objects.
In general, if you are performing a lot of operations with directories, use the DirectoryInfo
class. Once it is instantiated, the object has detailed information about the directory you are currently working on. In contrast, the Directory class is much simpler and is suitable if you are occasionally dealing with directories.
Working with Files Using the File and FileInfo Classes
The .NET Framework class library contains two similar classes for dealing with files — FileInfo
and
File.
The File
class provides static methods for creating, deleting, and manipulating files, whereas the FileInfo
class exposes instance members for files manipulation.
Like the Directory
class, the File
class only exposes static methods and does not contain any properties.
Consider the following program, which creates, deletes, copies, renames, and sets attributes in files, using the File
class:
static void Main(string[] args) {
string filePath = @'C: emp extfile.txt';
string fileCopyPath = @'C: emp extfile_copy.txt';
string newFileName = @'C: emp extfile_newcopy.txt';
try {
//---if file already existed---
if (File.Exists(filePath)) {
//---delete the file---
File.Delete(filePath);
}
//---create the file again---
FileStream fs = File.Create(filePath);
fs.Close();
//---make a copy of the file---
File.Copy(filePath, fileCopyPath);
//--rename the file---
File.Move(fileCopyPath, newFileName);
//---display the creation time---
Console.WriteLine(File.GetCreationTime(newFileName));
//---make the file read-only and hidden---
File.SetAttributes(newFileName, FileAttributes.ReadOnly);
File.SetAttributes(newFileName, FileAttributes.Hidden);
} catch (IOException ex) {
Console.WriteLine(ex.Message);
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
This program first checks to see if a file exists by using the Exists()
method. If the file exists, the program deletes it using the Delete()
method. It then proceeds to create the file by using the Create()
method, which returns a FileStream
object (more on this in subsequent sections). To make a copy of the file, you use the Copy()
method. The Move ()
method moves a file from one location to another. Essentially, you can use the Move()
method to rename a file. Finally, the program sets the ReadOnly
and Hidden
attribute to the newly copied file.