C # lesson Eighth LINQ Stream

Source: Internet
Author: User

1. Access to files and directories

① Accessing the Directory

You can instantiate DirectoryInfo to browse and access a particular directory (notice permission restrictions), and when you tap the code on the book, you find that there is a variable indentlevel if the directory name and its modified time are not available for looping output when initialized to 1 on the book. So I'm starting with a value of 1.

static int dircounter = 1; 2 static int indentlevel = 1;  3 public static void Main () 4 {5 Tester t = new Tester (); 6//start Directory 7 8 String thedirectory = Environment.getenvironmentvariable ("windir");              9 DirectoryInfo dir = new DirectoryInfo (thedirectory); T.exploredirectory (dir); 11 Console.WriteLine ("\n\n{0}directory found.\n", dircounter);}13 private void Exploredirectory (Dire              Ctoryinfo dir) 15 {16//push into a directory hierarchy indentlevel++;18//subdirectory indent 19 for (int i = 0; i < indentlevel; i++) {Console.WriteLine (""); Sole. WriteLine ("[{0}]{1} [{2}]\n], indentlevel, dir. Name, dir. LastAccessTime); directoryinfo[] directories = dir. GetDirectories (), DirectoryInfo Direc in directories 25 {26                     Dircounter++;27 exploredirectory (DIREC);//Recursively call yourself 28}29 Indentlevel--;30}31 32}

Some of the results are

You can also use Directory.getcurrentdiectory () to get the current directory (the Directory class method), I think this method will be very common.

The stream is used for IO processing, which has the following classes in the System.IO namespace: Binaryreader/writer Textreader/writer stream, where the class stream is an abstract class. There are three derived classes: MemoryStream: Read and write to memory BufferedStream: Read/write to buffer FileStream: Read and write to file

Textreader/writer is an abstract class. Derived from this class: Streamreader/streamwirter Stringreader/streamwrite

The IO operation basically requires a stream-dependent subclass. In fact, for the stream, the operation is relatively simple, as long as the details of the treatment of a little attention, I believe in the use of it can also be handy.

The use of the static void Main (string[] args) {//File stream:    Console.WriteLine ("Enter a path:");    String S=console.readline ();     FileStream filestream=new FileStream (s,filemode.openorcreate);     BinaryWriter binstream=new BinaryWriter (fileStream);     for (int i=1;i<=10;i++)     {        binstream.write (int) i),//writes a 4-byte signed integer to the current stream and increases the position of the stream by 4 bytes.    }     Binstream.close ();     Filestream.close ();     FileStream f=new             FileStream (s,filemode.open,fileaccess.read,fileshare.readwrite);     BinaryReader buf=new BinaryReader (f);     for (int i=1;i<=10;i++)     {    Console.WriteLine ("Output {0}", buf. ReadInt32 ()); This is possible, because int is a 32-bit    }}   

The output result is

About file and directory:

1. Classes provided for file and directory manipulation.

2. The classes you need is in the System.IO namespace.

3. File class:represents a file on disk.

4. Directory class:represents a directory (Windows folder).

Objects that inherit from this class can be referenced across application domain boundaries, or even remotely. When invoked remotely, a remote object is generated on-premises by a transparent proxy, which is invoked remotely by this proxy. The base class for objects that use proxy exchange messages to communicate across application domain boundaries.

Working with Directories:

1. The Directory class exposes static methods for creating, moving, and exploring directories.

2. All the methods of the Directory class is static; Therefore, you can call them all without have an instance of the class.

3. The DirectoryInfo class is a similar class, but one of the have nothing but instance members (I.e.,no static members at AL L).

4. The FileSystemInfo class has a number of properties and methods that provide information about a file or directory.

Creating a DirectoryInfo:

1. Object to explore a directory hierarchy, you need to instantiate a DirectoryInfo Object.

2. The DirectoryInfo class provides methods for getting don't just the names of contained files and directories, but also Fi Leinfo and DirectoryInfo objects, allowing you to dive on to the hierarchical structure, extracting subdirectories and exp Loring these recursively.

Working with Files:

1. The DirectoryInfo object can also return a collection of all the files in each subdirectory found.

2. The Directoryinfo.getfiles () method returns an array of FileInfo objects, each of which describes a file in that Direc Tory.

3. The FileInfo and File objects relate to one another, much as DirectoryInfo and Directory do.

4. Like the methods of Directory, the File methods is static.

5. Like DirectoryInfo, all the methods of FileInfo is instance methods.

C # lesson Eighth LINQ Stream

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.