C # Analysis of practical application examples of File Creation

Source: Internet
Author: User

Due to the openness of the Internet, C # is widely used to create files. For example, any file may be tampered during transmission, and the transmission process is uncertain, this makes it impossible for us to effectively guarantee the security of transmitted files. How can we find a solution to the problem? Here we will talk about the application that uses C # To create files.

To avoid the above situation, the most common practice is to provide a verification code in the C # File Creation along with the file transfer. After receiving the file, the user recalculates the file verification code and compares it with the original verification code, if they do not match, the file is changed during transmission. Next, I will use C # to demonstrate the specific implementation process.

C # create a file application 1. Create a filestream

Before building the file verification code, you must first load the file, which requires the filestream class of. NET Framework. In. NET Framework, all files are represented as a stream, which is the abstract concept of the byte sequence. All read/write operations involving files are implemented through the attributes and methods of the stream class. Below is the specific implementationCode:

 
  
  
  1. Filestream Fst =NewFilestream (
  2.  
  3. Txtfile. Text, filemode. Open,
  4.  
  5. Fileaccess. Read, fileshare. Read, 8192 );

Here, we use filestream to build a function. We need to mention that the last parameter value is 8192, which defines the buffer size, that is, when the file is larger than 8 K, in 8 K, the file is read in segments to improve the file reading performance.

C # create a file II. Create an MD5 object

After successfully creating a filestream object, you can use the MD5 class to calculate the hash value of the object. This implementation process is very simple. You only need to declare an md5cryptoserviceprovider instance first, then use the computehash method to complete the calculation process, and finally obtain the calculated byte array through the hash attribute.

  
  
  1. ......
  2. md5cryptoserviceprovider MD5 =
  3. New md5cryptoserviceprovider ();
  4. filestream Fst = New filestream (txtfile. text, filemode. open,
  5. fileaccess. Read, fileshare. Read, 8192);
  6. md5.computehash (FST);
  7. byte [] hash = md5.hash;
  8. ......

C # create a file 3. Convert the verification code string

Because MD5 HashAlgorithmThe returned data is a byte. Therefore, you must convert the data to a string. The following is the specific implementation code.

 
 
  1. ......
  2.  
  3. Byte[] Hash = md5.hash;
  4.  
  5. Stringbuilder sb =NewStringbuilder ();
  6.  
  7. Foreach(ByteBytInHash)
  8.  
  9. {
  10.  
  11. SB. append (string. Format ("{0: X1 }", BYT ));
  12.  
  13. }
  14.  
  15. Textbox1.text = sb. tostring ();
  16.  
  17. ......

During the conversion process, a stringbuilder object is defined first, which is mainly considered for performance. Then, each byte in the MD5 hash value is traversed and the string is used. the format method directly converts bytes to a hexadecimal string and outputs The result string.

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.