C # encapsulation of Binary File Processing

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data;
Using system. IO;

Namespace myquery. utils
{
/// <Summary>
/// Encapsulate Binary File Processing
/// By Jia Shiyi 2011-8-21
/// </Summary>
Public static class filehelper
{
/// <Summary>
/// Obtain file data
/// </Summary>
/// <Param name = "FILENAME"> complete path file name </param>
/// <Returns> </returns>
Public static byte [] getcontent (string filename)
{
Byte [] result = NULL;
If (file. exists (filename ))
{
Filestream FS = new filestream (filename, filemode. Open );
Result = new byte [fs. Length];
Int offset = 0;
While (offset <fs. length)
{
Int COUNT = 1024;
If (FS. Length <OFFSET + count)
{
Count = convert. toint32 (FS. Length-offset );
}
FS. Read (result, offset, count );
Offset + = count;
}
FS. Close ();
}
Return result;
}
/// <Summary>
/// Write content to a file
/// </Summary>
/// <Param name = "name"> complete file name </param>
/// <Param name = "content"> content </param>
/// <Param name = "iscover"> whether to overwrite existing files </param>
Public static void writetofile (string name, byte [] content, bool iscover)
{
Filestream FS = NULL;
Try
{
If (! Iscover & file. exists (name ))
{
FS = new filestream (name, filemode. append, fileaccess. Write );
}
Else
{
FS = new filestream (name, filemode. Create, fileaccess. Write );
}
Binarywriter BR = new binarywriter (FS );
BR. Write (content );
BR. Flush ();
BR. Close ();
}
Finally
{
If (FS! = NULL)
{
FS. Close ();
}
}

}
}
}

Welcome to: http: // 121.18.78.216, which is a demonstration platform for ease of query and analysis, workflow, content management, and project management.

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.