[Reading Notes] C # advanced programming Chapter 1 file and registry operations,

Source: Internet
Author: User

[Reading Notes] C # advanced programming Chapter 1 file and registry operations,

(1) files and registries

For file System operations, almost all related classes are in the System. IO namespace, and registry operations are handled by classes in the System. Win32 namespace.

 

 

(2) File System Management

  • System. externalbyrefobject -- this is the base object class used 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 -- the static members contained in this class can be used to process the Path name.
  • DriveInfo -- its attributes and methods provide information about the specified drive.

 

1... NET class indicating files and folders

The Directory class and File class only contain static methods and cannot be instantiated.

The DirectoryInfo and FileInfo classes implement the same public methods as the Directory class and File class, and have some public attributes and constructors, but they are stateful.

Ps: instances that need to perform multiple operations on the same file or maintain the status.

 

2. Path class

The Path class cannot be instantiated. It provides some static methods to perform operations on the path name more easily.

 

 

(3) Reading and Writing files

1. Stream

A stream is an object used for data transmission.

The most common classes for file read/write are:

  • FileStream (file stream)-This class is mainly used to read and write binary data in binary files-it can also be used to read and write any files.
  • StreamReader and StreamWriter-these two classes are used to read and write text files.

 

(4) files mapped to memory

Memory ing allows you to add part or all of the files to a virtual memory and use the file in the memory as the shared resources of multiple processes.

Static void Main (string [] args) {using (var mmFile = MemoryMappedFile. createFromFile (@ "C: \ Users \ Administrator \ Desktop \ test.txt", FileMode. create, "fileHandle", 1024*1024) {string valueToWrite = "write memory ing file"; // + DateTime. now. toString ("yyyy-MM-dd"); var myAccessor = mmFile. createViewAccessor (); myAccessor. writeArray <byte> (0, Encoding. default. getBytes (valueToWrite), 0, valueToWrite. length * 2); var readOut = new byte [valueToWrite. length * 2]; myAccessor. readArray <byte> (0, readOut, 0, readOut. length); var finalValue = Encoding. default. getString (readOut); Console. writeLine ("content:" + finalValue);} Console. readKey ();}

 

 

(5) Reading drive information

The DriveInfo class can scan the system and provide a list of available drives.

DriveInfo [] di = DriveInfo. getDrives (); foreach (DriveInfo itemDrive in di) {Console. writeLine ("============================="); Console. writeLine ("drive name:" + itemDrive. name); Console. writeLine ("ToString ():" + itemDrive); Console. writeLine ("available free space on the drive:" + itemDrive. availableFreeSpace); Console. writeLine ("file system name:" + itemDrive. driveFormat); Console. writeLine ("drive type:" + itemDrive. driveType); Console. writeLine ("indicates whether the drive is ready:" + itemDrive. isReady); Console. writeLine ("root directory of the drive:" + itemDrive. rootDirectory); Console. writeLine ("total available free space on the drive:" + itemDrive. totalFreeSpace); Console. writeLine ("total size of the bucket on the drive:" + itemDrive. totalSize); Console. writeLine ("Drive Volume:" + itemDrive. volumeLabel); Console. writeLine ("========================== ");}

 

 

(6) file security

1. Read the ACL from the file

Console. writeLine ("complete input file path:"); string myFilePath = Console. readLine (); try {using (FileStream myFile = new FileStream (myFilePath, FileMode. open, FileAccess. read) {FileSecurity fileSec = myFile. getAccessControl (); foreach (FileSystemAccessRule fileRule in fileSec. getAccessRules (true, true, typeof (NTAccount) {Console. writeLine ("{0} {1} {2} access {3}", myFilePath, fileRule. accessControlType = Acces SControlType. Allow? "Supported": "REJECTED", fileRule. FileSystemRights, fileRule. IdentityReference) ;}} catch (Exception) {Console. WriteLine ("an invalid file path is entered! ");}

 

 

(7) read and write the Registry

1. Registry

In the file system, the top node is the disk partition C: \, D: \, and so on. In the Registry, the top node is the Registry hive ), existing configuration units cannot be changed-a total of seven registry configuration units (but only five can be seen using regedit ):

  • Hkey_classes_root(hkcr)includes the segments (.txt).doc and so on) of the system files, and the applications that can be used to open each type of files. It also contains registration information for all COM components.
  • HKEY_CURRENT_USER (HKCU) includes the user configuration of the computer on which the user is currently logged on (desktop settings, environment variables, network and printer connections, and other settings that define the user's operating environment ).
  • HKEY_LOCAL_MACHINE (HKLM) is a large Configuration unit, which includes all the software and hardware information installed on the computer. It also contains HKCR configuration units: HKCR is not actually an independent Configuration unit, but a convenient ing to the registry key HKLM/SOFTWARE/Classes.
  • HKEY_USERS (HKUSR) includes the user preferences of all users. It also contains the HKCU Configuration unit, which is a ing of keys in HKEY_USERS.
  • HKEY_CURRENT_CONGFIG (HKCF) contains detailed information about hardware on the computer.

The remaining two keys contain temporary information, which is frequently changed:

  • HKEY_DYN_DATA is a general container that contains any volatile data that needs to be stored in the registry.
  • HKEY_PERFORMANCE_DATA contains information related to the performance of running applications.

The registry key can be formatted into one of the three data types:

  • REG_SZ (roughly equivalent to a. NET string)
  • REG_DWORD (roughly equivalent to. NET uint)
  • REG_BINARY (roughly equivalent to. NET byte [])

Applications create many keys in the Registry, which are usually stored in the HKLM \ Software \ <CompanyName> key.

 

2.. NET registry class

The RegistryKey class allows you to browse subkeys, create new keys, read or modify values in keys.

The Registry class can only perform a single access to the Registry key for simple operations. Another function is to provide a RegistryKey instance that represents the top-level key.

Obtain a RegistryKey instance that represents the HKLM key:

RegistryKey hklm = Registry. LocalMachine; // read the Windows Registry key HKEY_LOCAL_MACHINE

Read data from HKLM/Software/Microsoft:

RegistryKey hklm = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Microsoft");

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. The GetValue () method gets the value in the key:

RegistryKey hklm = Registry. localMachine. openSubKey ("Software", true ). createSubKey ("MyMicrosoft"); hklm. setValue ("MyValue", "Hello World"); // set the value of hklm. setValue ("MyValue2", 10); // set string myValue = (string) hklm. getValue ("MyValue"); // read the value int myValue2 = (int) hklm. getValue ("MyValue2"); // read the value

Finally, you need to disable the key:

hklm.Close();

Or use the using Keyword:

using (RegistryKey h=Registry.LocalMachine){}

 

 

(8) read/write independent memory

Independent storage can be viewed as a virtual disk, where data items can only be shared by applications that create them or other application instances.

Read and Write values from independent memory using ReadSettings () and SaveSetting () methods.

Independent memory, ing Memory Reference: http://blog.csdn.net/ghostbear/article/details/7328554

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.