. Net notes: an in-depth analysis of windows file operations using System. IO

Source: Internet
Author: User

Several Classes related to System File Processing in. Net are: File, Directory, FileInfo, DirectoryInfo, DriveInfo, FileSystemWatcher. This article describes the usage of these classes.
1. The File class provides static methods for creating, moving, copying, and deleting files, and opening File streams.
2. The Directory class provides static methods for creating, moving, copying, and deleting directories.
3. The FileInfo class uses class instances to create, copy, move, and delete files.
4. DirectoryInfo allows you to create, move, copy, and delete directories and enumerate subdirectories.
5. DriveInfo can obtain disk information in the windows operating system
6. FileSystemWatcher is used to monitor file or directory changes and trigger events.
7. The Path class provides static methods for file name and directory name operations
The usage of File, FileInfo, Directory, and DirectoryInfo classes is very simple and will not be described in detail.
1. How to Use DriveInfo to obtain windows System Disk Information
You are not allowed to construct DriveInfo instances in the program. You can use the static method GetDrives () of DriveInfo to obtain all disks in the windows system, including hard disks, cd and USB disks; note: when accessing the disk attribute, you must first determine whether the IsReady attribute is true. If the IsReady attribute is false, an exception is thrown when accessing some disk attributes. Example code: Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. IO;

Namespace aboutio
{
Class Program
{
Static void Main (string [] args)
{
DriveInfo [] drives = DriveInfo. GetDrives ();

Foreach (DriveInfo drive in drives)
{
If (drive. IsReady)
Console. writeLine ("type: {0} volume label: {1} Name: {2} total space: {3} remaining space: {4}", drive. driveType, drive. volumeLabel, drive. name, drive. totalSize, drive. totalFreeSpace );
Else
Console. WriteLine ("type: {0} is not ready", drive. DriveType );
}

Console. ReadLine ();
}
}
}

2. Use FileSystemWatcher to monitor Directories
FileSystemWatcher is used to monitor the modification, creation, and deletion of directories or files. To enable FileSystemWatcher to start monitoring, you must set its EnableRaisingEvents attribute to true, as shown in the following example:Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. IO;
Using System. Threading;

Namespace UseFileSystemWatcher
{
Class Program
{
Static void Main (string [] args)
{
// Declare the directory to be monitored
String watchPath = "D: \ watch ";
FileSystemWatcher watcher = new FileSystemWatcher (watchPath ,"*.*");
// Add a File Change Handling event
Watcher. Changed + = new FileSystemEventHandler (Watcher_Changed );
// Add a file creation event
Watcher. Created + = new FileSystemEventHandler (Watcher_Created );
// Add a file deletion event
Watcher. Deleted + = new FileSystemEventHandler (Watcher_Deleted );
// Add error handling
Watcher. Error + = new ErrorEventHandler (Watcher_Error );
// Start monitoring
Watcher. EnableRaisingEvents = true;
Thread. Sleep (1000*60 );
Console. WriteLine ("press any key to exit ..");
Console. Read ();
}

Static void Watcher_Error (object sender, ErrorEventArgs e)
{
Console. WriteLine ("error:" + e. ToString ());
}

Static void Watcher_Deleted (object sender, FileSystemEventArgs e)
{
Console. WriteLine (e. ChangeType + ":" + e. FullPath );
}

Static void Watcher_Created (object sender, FileSystemEventArgs e)
{
Console. WriteLine (e. ChangeType + ":" + e. FullPath );
}

Static void Watcher_Changed (object sender, FileSystemEventArgs e)
{
Console. WriteLine (e. ChangeType + ":" + e. FullPath );
}
}
}

3. The Path class provides a set of static methods for processing paths.
1) Path. GetDirectoryName (string path) returns the directory name. Note that whether a backslash exists at the end of the Path affects the result, as shown below:
Path. GetDirectoryName ("d: \ abc") will return d :\
Path. GetDirectoryName ("d: \ abc \") will return d: \ abc
2) Path. GetRandomFileName () returns a random file name.
3) Path. GetFileNameWithoutExtension ("d: \ abc.txt") will return abc
4) Path. GetInvalidPathChars () will return characters that are not allowed in the Path
5) Path. GetInvalidFileNameChars () will return characters that are not allowed in the file name.
6) Path. Combine (string left, string right) merge two paths
Note that the underlying windows APIs are called for the classes related to these file systems mentioned above, that is, these classes can only be used in windows systems, however, it is unavailable in other operating systems.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.