. Net: Windows File Operations in system. Io

Source: Internet
Author: User

System. Io series directory

1. Windows file directory Processing

2. Stream, streamreader, and streamwriter

3. compressed file

4. system. Io memory ing file shared memory

5. system. Io series: multiple threads in the LAN use named pipes to communicate between processes instance system. Io use pipelines to communicate between processes (system. Io. Pipes use)

6. Independent storage Zone

7. Port

Several Classes related to System File Processing in. NET are file, directory, fileinfo, directoryinfo, driveinfo, and 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:

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:

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 the 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 watcher for error handling. 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.

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.