C # Three Steps for reading file information

Source: Internet
Author: User

 

Link: http://developer.51cto.com/art/200908/145675.htm

 

In general C # development, how do you read file information through C? C # provides us with a very powerful class library (once again.. NET), which encapsulates almost all the classes we can think of and we don't think of. the stream is a general means of reading files, using stream reading. Yes, so are you sure you want to use it to read the data in the file? Can I really read it completely?

Generally, we use C # To Read File information using the following steps:

1. Declare and use the OpenRead of File to instantiate a File stream object, as shown below:

FileStream fs = File. OpenRead (filename );

Or

FileStream fs = FileStream (filename, FileMode. Open, FileAccess. Read, FileShare. Read );

2. Prepare a byte array for storing the file content. fs. Length will get the actual size of the file, as shown below:

Byte [] data = new byte [fs. Length];

3. Start reading. Call a method of a file stream to read data into the data array.

Fs. Read (data, 0, data. Length );

We can use C # to read the file information after only three sentences are written, and the content in the file is unblocked. It is really concise! Can this code really work as expected? The answer is: Almost yes!

In most cases, the above Code works well, but we should note that the Read method has a return value. Since there is a return value, it must be reasonable, if the preceding method is used, it can be a function without return values. The purpose of the returned value is to give us a chance to determine the actual size of the file to determine whether the file has been fully read. Therefore, the above Code cannot ensure that we have read all the bytes in the file (although in many cases it has been read ). The following method provides a safer method than the above method to ensure that the file is fully read.

           SafeRead(Stream stream,  offset =  remaining =             (remaining >  read = (read <=   EndOfStreamException(                   + read.ToString() +                 remaining -=                offset +=

 

In some cases, you do not know the actual length of a stream, for example, a network stream. In this case, you can use a similar method to read the stream until the data in the stream is fully read. We can initialize a cache first, and then write the stream information read from the stream to the memory stream, as shown below:

                      [] buffer =  [ (MemoryStream ms =                  ( read = stream.Read(buffer,                      (read <= 

 

Although the above example of implementing C # To Read File Information is relatively simple, and the effect is not very obvious (most of them are correct), maybe you will, it doesn't matter. This article was originally intended for beginners.

Let's take a look at the code example:

          [] Read2Buffer(Stream stream,              (BufferLen < =             [] buffer =   read =              ((block =- read)) >                 read +=                 (read ==                     nextByte =                     (nextByte == -                    [] newBuf =  [buffer.Length * = (= newBuf;                    read++            [] ret =  

 

The above method provides a way to read a Stream by specifying the cache length, although in many cases you can directly use Stream. length indicates the Length of the stream, but not all streams can. With this method, we can easily implement the simplicity and integrity of C # reading file information.

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.