. NET notes: An in-depth analysis of System.IO Windows file Operations _ Practical Tips

Source: Internet
Author: User
Tags error handling
Several classes related to processing system files in. NET are File, Directory, FileInfo, DirectoryInfo, DriveInfo, FileSystemWatcher。 This article describes the use of these several classes.
The 1.File class provides actions that static methods use to create, move, copy, and delete files, and to open file streams
The 2.Directory class provides actions that static methods use to create, move, copy, and delete directories
3.FileInfo classes implement to create, copy, move, and delete files with class instances
4.DirectoryInfo provides operations for creating, moving, copying, and deleting directories, and can enumerate subdirectories
5.DriveInfo access to disk information in the Windows operating system
6.FileSystemWatcher to monitor file or directory changes and raise events
The 7.Path class provides a static method for the operation of file name directory names
File, FileInfo, Directory, directoryinfo the use of these classes are very simple to do not repeat.
1. How to use DriveInfo to obtain Windows system disk information
You are not allowed to construct driveinfo instances of yourself in your program, you can Getdrives () all the disks in the Windows system via the DriveInfo static method (), including the hard disk, CD, and U disk Note that an exception is thrown when accessing a disk property by first determining whether its IsReady property is true,isready to some properties that access the disk. The following instance code:
Copy Code code 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 monitoring directory
FileSystemWatcher used to monitor directories or files for modification, creation, deletion, to enable FileSystemWatcher to start monitoring must set its EnableRaisingEvents property to True, as follows:
Copy Code code 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)
{
Declaring a directory to monitor
String Watchpath = "D:\\watch";
FileSystemWatcher watcher = new FileSystemWatcher (Watchpath, "*.*");
Add File Change handling events
Watcher. Changed + = new FileSystemEventHandler (watcher_changed);
Add File Create handle event
Watcher. Created + = new FileSystemEventHandler (watcher_created);
Add File Delete handle 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 name of the directory, needing to be aware that a backslash at the end of the path has an effect on the result, as follows:
Path.getdirectoryname ("D:\\abc") will return D:\
Path.getdirectoryname ("D:\\abc\") will return D:\ABC
2) Path.getrandomfilename () will return a random filename
3) Path. Getfilenamewithoutextension ("D:\\abc.txt") will return to ABC
4) Path.getinvalidpathchars () will return characters that are prohibited from being used in the path
5 Path. Getinvalidfilenamechars () returns characters that are prohibited from being used in file names
6) Path.Combine (string left,string right) merges two paths
It should be noted that the underlying layers of these file system-related classes mentioned above call the Windows API, which means that these classes can only be used under Windows systems and are not available under other operating systems.

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.