C # learning record 9--streams data stream

Source: Internet
Author: User
Tags try catch

The data flow in C # is much more powerful, in folder directory browsing, read-write file, asynchronous IO, asynchronous network programming, etc. can be used

1. Recursive Display of directories

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.IO;4 usingSystem.Text;5 6 namespaceStreamtester7 {8     classTester9     {Ten         Static intDircounter =1; One         Static intIndentLevel =-1; A  -          Public Static voidMain () -         { theTester T =NewTester (); -  -             //Select the root directory -             stringThedirectory = +Environment.getenvironmentvariable ("SystemRoot"); -             //If you are under other systems such as UNIX Linux, add the following sentence +             //string thedirectory = "/tmp"; A  at             //The following method is called to browse the directory, displaying the data and subdirectories -  -DirectoryInfo dir =NewDirectoryInfo (thedirectory); -  - t.exploredirectory (dir); -  in Console.WriteLine ( -                 "\n\n{0} directories found.\n", to dircounter); +         } -  the         //Just find a directory and call yourself recursively recursively. *  $         Private voidexploredirectory (DirectoryInfo dir)Panax Notoginseng         { -indentlevel++;//directory hierarchy + + the  +              for(inti =0; i < indentlevel; i++) AConsole.Write (" ");  the  +Console.WriteLine ("[{0}] {1} [{2}]\n", - IndentLevel, dir. Name, dir. LastAccessTime); $  $             //get all subdirectories under Dir to deposit directories[] -directoryinfo[] directories =dir. GetDirectories (); -             foreach(DirectoryInfo Newdirinchdirectories) the             { -dircounter++;WuyiExploredirectory (Newdir);//Recursive invocation the             } -indentlevel--;//Directory Hierarchy-- Wu         } -     } About}

Results

In the above results, the level of the table of contents is in parentheses. In the code is implemented by IndentLevel.

Note that you should use Try,catch when you are really using it. The reason for this is that there is a predictable/unavoidable problem: access to the folder. If you run without a try, exception stops running when you encounter an inaccessible folder.

2. Read and write to the file

1         Private voidRun ()2         {3             //Open File4FileInfo Thesourcefile =NewFileInfo (5                 @"C:\Users\shanyuwang\Desktop\test.txt");6 7             //Create Text Reader8StreamReader reader =Thesourcefile.opentext ();9 Ten             //Create text writer OneStreamWriter writer =NewStreamWriter ( A                 @"C:\Users\shanyuwang\Desktop\test2.txt",false); -  -             stringtext; the  -             //reads one line at a time and writes Text,writer -              Do -             { +Text =Reader. ReadLine (); - writer. WriteLine (text); + Console.WriteLine (text); A} while(Text! =NULL); at  -             //close the file. Recommend a good habit to develop and avoid mis-operation of files in subsequent programs - Reader. Close (); - writer. Close (); -}

The content in the result test2.txt is the same as in Test.txt.

Also be aware that the path to reader and writer is valid

3. Read the contents of the Web page

1  Public classClient2     {3         Static  Public voidMain (string[] Args)4         {5             //HTTP Request Request6HttpWebRequest WebRequest =7 (HttpWebRequest) webrequest.create8("http://www.baidu.com/");9 Ten             //HTTQ Response OneHttpWebResponse WebResponse = A (HttpWebResponse) webrequest.getresponse (); -  -             //Read the return contents of the response theStreamReader StreamReader =NewStreamReader ( - WebResponse.GetResponseStream (), encoding.ascii); -  -             Try +             { -                 stringoutputstring; +OutputString =streamreader.readtoend (); A Console.WriteLine (outputstring); at             } -             Catch -             { -Console.WriteLine ("Exception reading from web page"); -             } - streamreader.close (); in Console.readkey (); -         } to}

You can find that the returned result is an HTML file.

Note that the URL of the request is valid or not reached (YouTube, etc. will have exception)

It is very easy to see from the above example that it is often important to note that pointing to a path is valid, common try Catch

C # learning record 9--streams data stream

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.