(i) Documents and registration forms
For file system operations, the related classes are almost always in the System.IO namespace, while registry operations are handled by the classes in the System.win32 namespace.
(ii) Management of the document System
- system.marshalbyrefobject--this is. The Base object class for remote operations in the net class, which allows data to be grouped between application domains.
- filesysteminfo--this is the base class that represents any file system object.
- FileInfo and file--These classes represent files on the file system.
- DirectoryInfo and directory--These classes represent folders on the file system.
- path--This class contains static members that can be used to manipulate pathname names.
- driveinfo--its properties and methods provide information about the specified drive.
1. Represents files and folders. NET class
The directory class and the file class contain only static methods and cannot be instantiated.
The DirectoryInfo and FileInfo classes implement public methods that are roughly the same as the directory class and the file class, and have some public properties and constructors, but they are stateful.
Ps: Use instances that require multiple operations on the same file or need to maintain state.
2. Path class
The path class cannot be instantiated. It provides a number of static methods that make it easier to perform operations on pathname names.
(iii) Read and write documents
1. Flow
A stream is an object that is used to transfer numbers.
For file reads and writes, the most common classes are:
- 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.
- StreamReader (Stream reader) and StreamWriter (stream writer)-These two classes are dedicated to reading and writing text files.
(iv) files that map memory
Memory mapping allows a part or all of a file to be added to a virtual memory, and the file in memory can be used as a shared resource for multiple processes.
Static voidMain (string[] args) { using(varMmfile = Memorymappedfile.createfromfile (@"C: \users\administrator\desktop\test.txt", FileMode.Create,"FileHandle",1024x768*1024x768)) { stringValuetowrite ="writing memory-mapped files";//+ DateTime.Now.ToString ("Yyyy-mm-dd"); varMyaccessor =mmfile.createviewaccessor (); Myaccessor.writearray<byte> (0, Encoding.Default.GetBytes (Valuetowrite),0, valuetowrite.length*2); varReadout =New byte[valuetowrite.length*2]; Myaccessor.readarray<byte> (0, Readout,0, readout.length); varFinalvalue =Encoding.Default.GetString (readout); Console.WriteLine ("content:"+finalvalue); } console.readkey ();}
(v) Read drive information
The DriveInfo class can scan the system and provide a list of available drives.
driveinfo[] di =driveinfo.getdrives ();foreach(DriveInfo itemdriveinchdi) {Console.WriteLine ("===================="); Console.WriteLine ("name of the drive:"+itemdrive.name); Console.WriteLine ("ToString ():"+itemdrive); Console.WriteLine ("amount of free space available on the drive:"+itemdrive.availablefreespace); Console.WriteLine ("Name of file system:"+Itemdrive.driveformat); Console.WriteLine ("Drive Type:"+Itemdrive.drivetype); Console.WriteLine ("whether the drive is ready for the value:"+Itemdrive.isready); Console.WriteLine ("root directory of the drive:"+itemdrive.rootdirectory); Console.WriteLine ("Total free space available on the drive:"+itemdrive.totalfreespace); Console.WriteLine ("total size of storage space on drive:"+itemdrive.totalsize); Console.WriteLine ("volume label of the drive:"+Itemdrive.volumelabel); Console.WriteLine ("====================");}
(vi) Security of documents
1. Read the ACL from the file
Console.WriteLine ("Enter the full path of the file:");stringMyfilepath =console.readline ();Try{ using(FileStream myFile =NewFileStream (Myfilepath, FileMode.Open, FileAccess.Read)) {filesecurity filesec=Myfile.getaccesscontrol (); foreach(FileSystemAccessRule FileruleinchFilesec.getaccessrules (true,true,typeof(NTAccount))) {Console.WriteLine ("{ 0} {1} {2} access {3}", Myfilepath, Filerule.accesscontroltype = = Accesscontroltype.allow?"Support":"Reject", Filerule.filesystemrights, filerule.identityreference); } }}Catch(Exception) {Console.WriteLine ("Invalid file path was entered! ");}
(vii) Read and write the registration form
1. Registration Form
In the file system, the topmost node is the partition of the disk, C: \, D:\, and so on. In the registry, the topmost node is the registry hive (Registry hive), and the existing hive is immutable-there are 7 registry hives (but only 5 can be seen using Regedit):
- HKEY_CLASSES_ROOT (HKCR) contains the details of the file types on the system (. txt,. doc, and so on), as well as using those applications to open each type of file. It also contains registration information for all COM components.
- HKEY_CURRENT_USER (HKCU) contains user Configuration (desktop settings, environment variables, network and printer connections, and other settings that define the user's operating environment) for the computer that the user is currently logged on to.
- HKEY_LOCAL_MACHINE (HKLM) is a large configuration unit that includes all the software and hardware information that is installed on the computer. It also contains the HKCR hive: HKCR is not actually a separate hive, but a convenient mapping to the registry key hklm/software/classes.
- HKEY_USERS (HKUSR) contains user preferences for all users. It also contains the HKCU hive, which is a mapping of a key in the HKEY_USERS. HKCU Hive.
- Hkey_current_congfig (HKCF) contains detailed information about the hardware on your computer.
The remaining two keys contain temporary information that changes frequently:
- Hkey_dyn_data is a generic container that contains any volatile data that needs to be stored in the registry.
- The hkey_performance_data contains information related to the performance of the running application.
A registry key can be formatted as one of 3 data types:
- REG_SZ (roughly equivalent to. NET of String)
- REG_DWORD (roughly equivalent to. NET of UINT)
- REG_BINARY (roughly equivalent to. NET of byte[])
The application creates many keys in the registry, usually stored in the hklm\software\<companyname> key.
2,. NET Registry class
The methods implemented by the RegistryKey class can browse subkeys, create new keys, read or modify values in keys.
The registry class can only have single access to the registry keys to perform simple operations, and the other is to provide a RegistryKey instance that represents the top-level key.
Get a RegistryKey instance that represents the HKLM key:
RegistryKey HKLM = Registry.localmachine; // Read the Windows registry base key HKEY_LOCAL_MACHINE
Read the data in the Hklm/software/microsoft key:
RegistryKey HKLM = Registry.LocalMachine.OpenSubKey ("software"). OpenSubKey ("Microsoft");
To modify the Hklm/software/microsoft key:
RegistryKey HKLM = Registry.LocalMachine.OpenSubKey ("software"). OpenSubKey ("Microsoft"true);
Create a key using the CreateSubKey () method:
RegistryKey HKLM = Registry.LocalMachine.OpenSubKey ("software",true). CreateSubKey ("mymicrosoft");
The SetValue () method sets the data in the key, and the GetValue () method gets the value in the key:
RegistryKey HKLM = Registry.LocalMachine.OpenSubKey ("Software",true). CreateSubKey ("Mymicrosoft"); HKLM. SetValue ("myvalue","Hello World");//Setting the valueHklm. SetValue ("MyValue2",Ten);//Setting the valuestringMyValue = (string) HKLM. GetValue ("myvalue");//Read ValueintMyValue2 = (int) HKLM. GetValue ("MyValue2");//Read Value
Finally, you need to close the key:
Hklm. Close ();
or use the Using keyword:
using (RegistryKey h=registry.localmachine) {}
(eight) read-write Independent memory
Stand-alone storage can be thought of as a virtual disk in which you can save data items that can be shared only by the application that created them or by other application instances.
Read and write values from isolated memory using the readsettings () and SaveSetting () methods.
Standalone memory, mapped memory reference:http://blog.csdn.net/ghostbear/article/details/7328554
"Reading notes" C # Advanced Programming Chapter 24th file and Registry operations