1,directory class and File class
Contains only static methods and cannot be instantiated. You can use these classes whenever you invoke a member method that provides the path to the appropriate file system object. If you only perform an action on a folder or file, it is useful to use these classes because it saves you from instantiating. NET class is a system overhead.
File.Copy (@ "C:\1.txt",@ "D:\copy.txt");
2,directoryinfo class and FileInfo class
These two classes implement public methods that are roughly the same as the directory class and file, and have some public properties and public functions, but they are stateful and the members of those classes are not static. You need to instantiate these classes, and then associate each instance with a specific folder or file. If you use the same object to perform multiple operations, it is more efficient to use these classes.
New FileInfo (@ "C:\1.txt"); Myfile.copyto (@ "D:\copy.txt");
3,path class
The path class cannot be instantiated. However, it provides a number of static methods that make it easier to perform operations on pathname names.
For example, to display the full pathname of a ReadMe.txt file in a folder C:\My documents, you can find the path to the file using the following code:
Console.WriteLine (Path.Combine (@ "C:\My Documents","ReadMe.txt "));
4. Move to, copy, and delete files
Moving and deleting files or folders can be done using the MoveTo () and Delete () methods of the FileInfo and DirectoryInfo classes.
The two corresponding methods of the file and directory classes are move () and delete ().
The FileInfo and file classes also implement the CopyTo () and copy () methods, respectively.
Instead of copying the full folder, copy each file in the folder.
5, read and write files
In the. NET Framework 4.5, you can read and write files through a file object.
Read:
TextBox2.Text = File.readalltext (TextBox1.Text);
Write:
File.writealltext (Textbox1.text,textbox2.text);
6, Stream
A stream is an object that transmits data that can be transmitted in two directions:
1) If the data is transferred from an external source to the program, this is the read stream.
2) If the data is transferred from the program to an external source, this is the write stream.
The most common classes for reading and writing files are as follows:
FileStream (file stream)-This class is primarily used to read and write binary data in binary files--and can also be used to read and write to any file.
Stramreader (Stream reader) and StreamWriter (stream writer)-These two classes are dedicated to reading and writing text files.
New StreamReader (@ "C:\My documents\readme.txt
Link the StreamReader to the FileStream. The advantage is that you can specify whether to create files and share licenses.
New FileStream (@ "C:\My documents\readme.txt", FileMode.Open,FileAccess.Read, Fileshare.none); StreamReader SRnew StreamReader (FS);
Get StreamReader from a FileInfo instance:
New FileInfo (@ "C:\1.txt"); = Myfile.opentext ();
The StreamWriter class works like StreamReader, but StreamWriter can only be used to write to a file (or another stream).
7. Read drive information
Read drive information can be implemented using the DriveInfo class. The class can scan the system, provide a list of available drives, and further provide a lot of detail on any drive.
8, read and write the registration form
The registry is the core repository that contains all of the configuration information for Windows installation, user preferences, and your own installation of software and devices.
To access the registry, you can use the two classes registry and RegistryKey in the Microsoft.Win32 namespace. A RegistryKey instance represents a registry key. This class implements methods that can browse subkeys, create new keys, read or modify values in a key. In other words, the class can usually complete all operations on the registry key.
The registry class can only access the registry keys for a single visit to perform simple operations. Another function of the registry class is to provide a registrykey example that represents a top-level key (a different hive) so that you can start locating in the registry.
If you want to read some of the data in the Hklm/software/microsoft key, you can use the following code to get a reference to it:
RegistryKey HKLM = registry.localmachine; = HKLM. OpenSubKey ("software"); = Hksofeware.opensubkey ("Microsoft");
If you are creating a key, you should use the CreateSubKey () method, which automatically provides read and write access to the key through the returned reference:
RegistryKey HKLM = registry.localmachine; = HKLM. OpenSubKey ("software"); = Hksofeware.createsubkey ("myownsoftware");
How the CreateSubKey () method works: If the key does not exist, it creates the key. If the key already exists, it returns a RegistryKey instance that represents the key.
C # Advanced Programming book Notes (16): File and Registry operations